Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8074075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:32:14+00:00 2026-06-05T14:32:14+00:00

I have a string -4.00 % which I need to convert to a decimal

  • 0

I have a string “-4.00 %” which I need to convert to a decimal so that I can declare it as a variable and use it later. The string itself is found in string[] rows. My code is as follows:

foreach (string[] row in rows)
{
string row1 = row[0].ToString();
Match rownum = Regex.Match(row1.ToString(), @"\-?\d+\.+?\d+[^%]");
string act = Convert.ToString(rownum); //wouldn't convert match to decimal
decimal actual = Convert.ToDecimal(act);
textBox1.Text = (actual.ToString());
}

This results in “Input string was not in a correct format.” Any ideas?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T14:32:15+00:00Added an answer on June 5, 2026 at 2:32 pm

    Here’s a modified version of your code that changes the regex to use a capturing group and explicitly look for a %. As a consequence, this also simplifies the parsing to decimal (no longer need an intermediary string):

    EDIT : check rownum.Success as per executor’s suggestion in comments

    string[] rows = new [] {"abc -4.01%", "def 6.45%", "monkey" };
    
    foreach (string row in rows)
    {
        //regex captures number but not %
        Match rownum = Regex.Match(row.ToString(), @"(\-?\d+\.+?\d+)%");
    
        //check for match
        if(!rownum.Success) continue;
    
        //get value of first (and only) capture
        string capture = rownum.Groups[1].Value;
    
        //convert to decimal
        decimal actual = decimal.Parse(capture);
    
        //TODO: do something with actual
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following string which i need to convert to integer or bigint.
I have a whole XML document in a String which i need to convert
I have a binary string, entered by the user, which I need to convert
I have a string which holds a decimal value in it and I need
I have the following string: 3.39112632978e+001 which I need to convert to float. WolframAlpha
I have a base32 string which I need to convert to a byte array.
I have the following string áéíóú which I need to convert it to aeiou
I have an html form which contains a String which I need to convert
I have got the String MacAddress, which i need to convert in to a
I have a string which I need to verify if it's a Country code.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.