简体   繁体   中英

Double.Parse cannot keep comma

I'm trying to convert a string into double, I've this value: 53.095 and try to convert into double as:

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

I get: 53095 . Why I doesn't get the comma? What am I missing?

I guess it's because different countries handle comma differently. My country, for example uses , instead. So you must be aware of how the string is formatted.

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

For another localization this will work.

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

EDIT:

As king_nak mentioned, you will be able to use CultureInfo.InvariantCulture as long as you're using the english style for formatting.

[...] it is associated with the English language but not with any country/region.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM