[英]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: 我正在尝试将字符串转换为double,我有这个值: 53.095
并尝试转换为double为:
string doub = "53.095";
var cv = Convert.ToDouble(doub);
I get: 53095
. 我得到: 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. 正如king_nak所提到的,只要你使用英文风格进行格式化,你就可以使用CultureInfo.InvariantCulture
。
[...] 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.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.