Costel
Costel

Reputation: 43

Conversion of a string to double C#

I have an interesting problem with a specific conversion. When I try to convert the string "0,3" or "0.3", according to the UICulture, to a Double value, the result is 0,29999999. I have not yet found a solution, in order to receive the result 0,3.

There is any way to have the same values after the conversion?

Upvotes: 3

Views: 241

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1062780

double cannot represent every value. It guarantees to represent integers, but that is about it. If you need something more like "human" approximation of numbers, use decimal:

decimal val = decimal.Parse("0.3");

Note: decimal also doesn't represent every value - but the way it does the approximation tends to be more like how people expect numbers to work. In particular, double is virtually useless for things like currency.

Upvotes: 14

Related Questions