Getting Started with Adobe After Effects - Part 6: Motion Blur
First Time? You can support us by signing up. It takes only 5 seconds. Click here to sign up. If you already have an account, click here to login.
Loading

1st Prize - Apple iPad


DOTNET Quiz 2011 - Convert a string containing formatted number with comma.

  • A function is returning a string containing a formatted number with comma.

    eg. String svalue = GetCurrentPrice(); returns 11,200

    You want to convert this value into a valid number to performs some calculations. eg. 11200+100

    How do I convert it with mininum efforts.

    Posted on 01-25-2011 00:00 |
    Jignesh Desai
    142 · 1% · 338

9  Answers  

Subscribe to Notifications
  • Score
    2

    string amt = "4,560";
    // Replace comma with EmptyString
    amt=amt.Replace(",",string.Empty);
    //Then Convert
    int amount = Convert.ToInt32(amt);

    Replied on Jan 25 2011 12:14AM  . 
    Jai
    2276 · 0% · 5
  • Score
    8

    I believe you can use Parse method. For instance, if you wan to parse a string that contains integer value, you can use:

    Int32.Parse(svalue, NumberStyles.AllowThousands,NumberFormatInfo.InvariantInfo)
    

    NumberFormatInfo.InvariantInfo uses comma as a group separator. You can also use Int32.TryParse.

    If you want to parse double string, you can use Double.Parse instead. But you'd have to add appropriate flag to allow decimal points.

    Replied on Jan 25 2011 12:23AM  . 
    Maciej Pakulski
    340 · 0% · 120
  • Score
    5

    You can use:

    string svalue = "11,200.256";

    txtResult.Text = decimal.Parse(svalue, CultureInfo.InvariantCulture.NumberFormat).ToString("F");

    Replied on Jan 25 2011 5:54AM  . 
    hitesh Gopani
    2276 · 0% · 5
  • Score
    10

    To accept a comma as thousands-separator you sghould explicitly use a culture info where this is true. In german culture, for example, the thousands separator is a dot, whereas the comma depicts the decimal separator. So the following line interprets the comma as a thousands separator :

    int someValue = int.Parse("13,200", NumberStyles.AllowThousands, CultureInfo.GetCultureInfo("en-us"));
    

    If you want to allow decimals, you should use the AllowSeparator flag too; for leading signs, the AllowLeadingSign. A standard currency value can be converted using the following snippet

    double someValue = double.Parse("13,200", NumberStyles.Currency, CultureInfo.GetCultureInfo("en-us"));
    
    Replied on Jan 26 2011 2:53AM  . 
    Guenter
    28 · 6% · 1838
  • Score
    7

    You can use the appropriate NumberStyle:

    string svalue = GetCurrentPrice();
    long value = long.Parse(svalue, System.Globalization.NumberStyles.AllowThousands);

    I've used long here for demo purposes only. Depending on the type of actual data returned by the function and what is required in the calculations, you can use Integer, Single, Double or any other appropriate number type.

    Replied on Jan 26 2011 6:29AM  . 
    Pradeep Kumar
    299 · 0% · 145
  • Score
    2

    Use TryParse to determine if the conversion succeeed.

    private string CalculateCost(int quantity)
        {
            String svalue = GetCurrentPrice();            
    
            float price = 0;
            if (Single.TryParse(svalue, out price))
            {
                return (price * quantity).ToString("C");
            }
            else return "Unable to get current price";
        }
    
    Replied on Feb 14 2011 8:02AM  . 
    pshurtleff
    1259 · 0% · 19
  • Score
    0

    Which one is the correct answer?

    Replied on Feb 14 2011 9:16PM  . 
    foluis
    2276 · 0% · 5
  • Score
    2

    If we have a string, and we expect it to always be an integer best practice is to use Int32.Parse(). If we are collecting input from a user, we can generally use Int32.TryParse(), since it allows more fine-grained control over the situation when the user enters in invalid input.

    Convert.ToInt32() takes an object as its argument, and it invokes Int32.TryParse() when it finds that the object taken as the argument is a string. Convert.ToInt32 also does not throw ArgumentNullException when it's argument is null the way Int32.Parse() does. That also means that Convert.ToInt32() is probably a wee bit slower than Int32.Parse() because it has to ask its argument what it's type is.

    Convert.ToInt has performance penality. Here is the best practice code

    public static int ToInt32(string value)
     {  
       if (value == null) 
        {     
        return 0;   
        }     
    return int.Parse(value, CultureInfo.CurrentCulture);
     }
    
    Replied on Feb 23 2011 1:03AM  . 
    Vamshi
    132 · 1% · 376
  • Score
    10

    Let's assume the function will always return valid number, so we are not going to check for errors or special cases. Let's also assume that the function returns integer, so I'll do all my code samples for integer. If it's not integer then the code provided below can be easily changed to work with the other types.

    If you can guarantee that NumberFormatInfo for current culture in the code where you parse the string will always have "," sign as NumberGroupSeparator then the easiest way to parse the result of GetCurrentPrice() function will be:

    int.Parse(svalue, NumberStyles.AllowThousands);
    

    If you cannot guarantee that NumberFormatInfo for the current culture in the code where you parse the string will always have "," sign as NumberGroupSeparator then I believe the easiest way to do the task is:

    int.Parse(svalue, NumberStyles.AllowThousands, NumberFormatInfo.InvariantInfo);
    

    This will work as NumberFormatInfo.InvariantInfo has "," as NumberGroupSeparator.

    Also note that the function name is GetCurrentPrice, so if it will ever be changed to return currency values we can pretty easy modify the code to accommodate the change.

    Replied on Mar 25 2011 3:23PM  . 
    Dmitry Kharlap (aka Docker)
    148 · 1% · 325

Your Answer


Sign Up or Login to post an answer.
Please note that your answer will not be considered as part of the contest because the competition is over. You can still post an answer to share your knowledge with the community.

Copyright © Rivera Informatic Private Ltd.