Convert string to double c #
I have an interesting problem with a specific transformation. When I try to convert a string "0,3"
or "0.3"
, according to a UICulture
value Double
, the result is 0,29999999
. I haven't found a solution yet to get the result 0,3
.
Is there any way to have the same values ββafter conversion?
source to share
double
cannot represent every value. It guarantees integer representation, but that's about it. If you want something more like a "human" approximation of numbers, use decimal
:
decimal val = decimal.Parse("0.3");
Note: decimal
Doesn't represent every value either, but the way it approximates tends to be more like how people expect numbers to work. In particular, it's double
practically useless for things like currency.
source to share