1

How to get correct double digit from string.

string first = "23.3";
string second = "23,3";

For now I used the sample parser for parse the number in double format:

double number = double.Parse(first);
double another = double.Parse(second);

So if I used en-US culture and for decimal separator used '.' then the result will be number = 23.3 and another = 233.

So my question is do is possible to ignore the decimal separator and when is parse in both case to return result = 23.3.

Alexey Zimarev
  • 17,944
  • 2
  • 55
  • 83
evelikov92
  • 755
  • 3
  • 8
  • 30
  • Can't you simply replace the ',' character in the second string with '.'? – 3615 Apr 06 '16 at 06:17
  • Yeah but some times is working with comma and some times with point. I don't know what decimal separator is used – evelikov92 Apr 06 '16 at 06:19
  • 1
    Possible duplicate of [Parse strings to double with comma and point](http://stackoverflow.com/questions/11560465/parse-strings-to-double-with-comma-and-point) – fubo Apr 06 '16 at 06:19
  • I don't think that it's a problem, because when you got point, it's remains unchanged, while comma will be replaced with dot. – 3615 Apr 06 '16 at 06:20
  • 3
    What would be the result of "10,230" ? 10 thousand 230, or 10.23? The answer is ambiguous unless you know what the decimal separator is. – Rob Apr 06 '16 at 06:20
  • @Rob if decimal separator is ',' then is to be 10.23. In another case I just not parse because I working with small numbers from -20 to 100 – evelikov92 Apr 06 '16 at 06:23
  • And with inputs like `"1,234.567"` or `"1.234,567"` even `string.Replace` won't save you anymore. – Corak Apr 06 '16 at 06:33
  • @Corak My most biggest number in the application is possible to be 100 and most less number is possible to be -20. Whit thousands I'm not operated. If user add 1,234.567 or 1.234,567 is cannot doing nothing. – evelikov92 Apr 06 '16 at 06:37
  • 1
    @evelikov92 - Yes, I just wanted to point out that while `string.Replace` works in your case, it is not a general solution for parsing a string with a decimal separator to a double. – Corak Apr 06 '16 at 06:48

2 Answers2

3

In addition to replace comma with dot, you need to supply a proper number format:

public double ParseMyString(string myString)
{
    return double.Parse(myString.Replace(',', '.'), 
        new NumberFormatInfo() {NumberDecimalSeparator = "."});
}

Another option to replace the separator on a broader scope is to use this:

Thread.CurrentCulture.NumberFormat.NumberDecimalSeparator = ".";

You will still need to replace comma with dot though.

Alexey Zimarev
  • 17,944
  • 2
  • 55
  • 83
0

You can play a trick:

    private string ReplaceSeparator(string Num)
    {
        return Num.Replace(",", CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator).Replace(".", CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator);
    }

    .....

string first = "23.3";
string second = "23,3";

first = ReplaceSeparator(first);
second = ReplaceSeparator(second);

double number = double.Parse(first);
double another = double.Parse(second);
Anton Semenov
  • 6,227
  • 5
  • 41
  • 69