简体   繁体   English

奇怪的解析Double行为

[英]strange parsing Double behaviour

I have this line of code: 我有这行代码:

return (this.pretWithoutDiscount / Double.Parse(UtilsStatic.getEuroValue())).ToString("N2") + "€";

In debug mode i've tested and the values are: 在调试模式下,我已经测试过,其值为:

UtilsStatic.getEuroValue() = "4.1878" UtilsStatic.getEuroValue()=“ 4.1878”

this.pretWithoutDiscount = 111.0 this.pretWithoutDiscount = 111.0

Can anyone explaing WHY: 谁能解释为什么:

Double.Parse(UtilsStatic.getEuroValue()) = 41878.0 Double.Parse(UtilsStatic.getEuroValue())= 41878.0

when it should be 4.1878 ?? 什么时候应该是4.1878

Thanks... 谢谢...

PS: UtilsStatic.getEuroValue returns a string. PS:UtilsStatic.getEuroValue返回一个字符串。

What decimal seprator are you using, my guess would be ','. 您使用的是十进制分隔符,我的猜测是“,”。 Try 尝试

Double.Parse(UtilsStatic.getEuroValue(), 
             System.Globalization.CultureInfo.InvariantCulture) 

which will parse the number using '.' 它将使用“。”解析数字 as the decimal separator. 作为小数点分隔符。

看来您正在处理金钱,因此应使用decimal类型而不是double

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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