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?

+3


source to share


1 answer


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.

+14


source







All Articles