Parse Strings to Double With Comma and Point

Parse strings to double with comma and point

You want to treat dot (.) like comma (,). So, replace

if (double.TryParse(values[i, j], out tmp))

with

if (double.TryParse(values[i, j].Replace('.', ','), out tmp))

Convert a string with commas to double

There are a lot of possible solutions. Although, the best one I think is to set your CultureInfo to Invariant and then replace all commas with dots. For example:

Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
string number = "3,2";
double d = double.Parse(number.Replace(",", "."));
Console.WriteLine(d);

In your code that would be made like this:

    double[] numbers1 = 
lineStr1.Replace(",", ".")
.Split(new char[0], StringSplitOptions.RemoveEmptyEntries)
.Select(s =>
{
double value;
bool success = double.TryParse(s, NumberStyles.Any, CultureInfo.InvariantCulture, out value);

return new { value, success };
})
.Where(p => p.success)
.Select(p => p.value)
.ToArray();

double[] numbers2 =
lineStr2.Replace(",", ".")
.Split(new char[0], StringSplitOptions.RemoveEmptyEntries)
.Select(s =>
{
double value;
bool success = double.TryParse(s, NumberStyles.Any, CultureInfo.InvariantCulture, out value);

return new { value, success };
})
.Where(p => p.success)
.Select(p => p.value)
.ToArray();

Now you have two arrays numbers1 and numbers2 with all possible numbers parsed from the two strings lineStr1 and lineStr2. You decide what to do with them as I cannot understand fully the purpose of your method.

(Thanks to all the guys in the comments for making me edit my answer.)

Parsing double with dot to comma

You, probably, should either provide "nl-NL" whenever you work with Netherlands' culture

  var calResult = 15.2d;
var calResultString = calResult.ToString(CultureInfo.GetCultureInfo("nl-NL"));
// We should parse with "nl-NL", not with CurrentCulture which seems to be "en-US"
var result = double.Parse(calResultString, CultureInfo.GetCultureInfo("nl-NL"));

Or specify CurrentCulture (default culture)

  CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("nl-NL");

var calResult = 15.2d;
// now CultureInfo.GetCultureInfo("nl-NL") is redundant
var calResultString = calResult.ToString();
var result = double.Parse(calResultString);

Finally, if you have a string which represents some floating point value in en-US culture, and you want the same value but be a string in nl-NL format:

  string source = "123.456";

string result = double
.Parse(source, CultureInfo.GetCultureInfo("en-US"))
.ToString(CultureInfo.GetCultureInfo("nl-NL"));

How to parse String, with both decimal separators comma and dot as well, to Double

Like Mohit Thakur said, but compilable.

NumberFormat format = NumberFormat.getInstance(Locale.FRANCE);
try {
Number number = format.parse(formDto.getWeight().replace('.', ','));
kitten.setWeight(number.doubleValue());
} catch (ParseException e) {
e.printStackTrace();
}

How do I parse string with decimal separator to a double c#?

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.

How can I convert String with comma to double in Dart?

For the answer to your final question:

String t = '5,000';
double f = double.parse(t.replaceAll(',',''));
print(f);

Use .replaceAll() to replace all instances of the comma with no character

Best way to parseDouble with comma as decimal separator?

Use java.text.NumberFormat:

NumberFormat format = NumberFormat.getInstance(Locale.FRANCE);
Number number = format.parse("1,234");
double d = number.doubleValue();

Updated:

To support multi-language apps use:

NumberFormat format = NumberFormat.getInstance(Locale.getDefault());


Related Topics



Leave a reply



Submit