Double.Parse cannot keep a comma

I am trying to convert a string to double, I have this value: 53.095and try to convert to double as:

string doub = "53.095";
var cv = Convert.ToDouble(doub);

I get: 53095. Why am I not getting a comma? What am I missing?

+4
source share
1 answer

I guess because different countries treat commas differently. In my country, for example, it is used instead ,. Therefore, you should know how the string is formatted.

string doub = "53.095";
var cv = double.Parse(doub, new CultureInfo("en-GB"));

For a different localization, this will work.

string doub = "53,095"; // note ,
var cv = double.Parse(doub, new CultureInfo("sv-SE"));

EDIT:

As pointed out by king_nak , you can use CultureInfo.InvariantCultureit while you use the English style for formatting.

[...] , - /.

string doub = "53.095";
string doub2 = "53,095"; 
var cv1 = double.Parse(doub, CultureInfo.InvariantCulture); // Works
var cv2 = double.Parse(doub2, CultureInfo.InvariantCulture); // Does not work.
+9

Source: https://habr.com/ru/post/1656341/


All Articles