[英]parsing a string value to double with comma decimal seperator
if there is any one who find a solution to this 如果有人找到解决方案
string x = "7,50";
string y = "5";
double a = double.Parse(x);
double b = double.Parse(y);
double c = a - b;
then the result must be 2,50. 那么结果必须是2,50。
but I got 70. because of decimal point x is treated as 75. 但是我得到了70。因为小数点x被视为75。
Just specify the appropriate culture to double.Parse
. 只需指定适当的文化来double.Parse
。 For example: 例如:
CultureInfo french = new CultureInfo("fr-FR");
double x = double.Parse("7,50", french);
I suspect you actually had "7,5" as a value, however - as "7,50" would be parsed as "750" if you were using a culture which didn't use comma as the separator. 我怀疑您实际上有一个“ 7,5”作为值,但是-如果您使用的文化没有使用逗号作为分隔符,则“ 7,50”将被解析为“ 750”。
Of course, if these are currency values you should consider using decimal
instead of double
to start with... 当然,如果这些是货币值,则应考虑使用decimal
而不是double
开头...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.