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 like 3.9 I want to convert this string in to
I have a number as a string like this: 9.756088256835938E-4 but I only can
HI all, i have a date string like this 2011-03-31 13:32:02 i want to
I have a class along the lines of: public class Observation { private String
I have a SQL like this. SELECT A.HESAP_NO, A.TEKLIF_NO1 || '/' || A.TEKLIF_NO2 AS
What is the safe/portable way to convert a number to a string (and the
I want to convert each letter in a sentence to a certain letter depending
Here's my problem (I'm programming in C): I have some huge text files containing
I want to pass a serialized json object and returned it within custom Html
I surfed through existant questions regarding this theme, but my issue is little specific

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.