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 580073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:26:02+00:00 2026-05-13T14:26:02+00:00

I have a string like 1.5% and want to convert it to double value.

  • 0

I have a string like “1.5%” and want to convert it to double value.

It can be done simple with following:

public static double FromPercentageString(this string value)
{
    return double.Parse(value.SubString(0, value.Length - 1)) / 100;
}

but I don’t want to use this parsing approach.

Is any other approach with IFormatProvider or something like this?

  • 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-05-13T14:26:03+00:00Added an answer on May 13, 2026 at 2:26 pm

    If you care about catching formatting errors, I would use TrimEnd rather than Replace. Replace would allow formatting errors to pass undetected.

    var num = decimal.Parse( value.TrimEnd( new char[] { '%', ' ' } ) ) / 100M;
    

    This will ensure that the value must be some decimal number followed by any number of spaces and percent signs, i.e, it must at least start with a value in the proper format. To be more precise you might want to split on ‘%’, not removing empty entries, then make sure that there are only two results and the second is empty. The first should be the value to convert.

    var pieces = value.Split( '%' );
    if (pieces.Length > 2  || !string.IsNullOrEmpty(pieces[1]))
    { 
        ... some error handling ... 
    }
    var num = decimal.Parse( pieces[0] ) / 100M;
    

    Using Replace will allow you to successfully, and wrongfully IMO, parse things like:

    • %1.5
    • 1%.5
    • 1.%5

    in addtion to 1.5%

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string that looks like this: 9/1/2009. I want to convert it
I have a string like this: (list:\RTM API\ status:completed) And I want to convert
I have a hex string like: data = 437c2123 I want to convert this
I have a string like 3.9 I want to convert this string in to
I have string like 123-123-1234 so I want to convert string into this format
I have string looking like this: 'Toy Story..(II) (1995)' I want to split the
I have a string like 'apples'. I want to find this string, and I
I have a string like this in C#: string a = A|B|C|D I want
I have code like: lblFranshizShowInvwNoskhehEdit.Text = string.Format({0:n}, (double)(int.Parse(drDarman[FranshizDarsad].ToString()) * Convert.ToInt64(RadNumerictxtPayInvwNoskhehEdit.Text)) / 100); But {0:n0}
I have a string +1.29% or -1.29% How can I convert this into a

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.