开发者

strange parsing Double behaviour

开发者 https://www.devze.com 2022-12-31 14:51 出处:网络
I have this line of code: return (this.pretWitho开发者_如何学CutDiscount / Double.Parse(UtilsStatic.getEuroValue())).ToString("N2") + "€";

I have this line of code:

return (this.pretWitho开发者_如何学CutDiscount / Double.Parse(UtilsStatic.getEuroValue())).ToString("N2") + "€";

In debug mode i've tested and the values are:

UtilsStatic.getEuroValue() = "4.1878"

this.pretWithoutDiscount = 111.0

Can anyone explaing WHY:

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

when it should be 4.1878 ??

Thanks...

PS: UtilsStatic.getEuroValue returns a string.


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.


It seems you are dealing with money, so decimal type should be used instead of double.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号