1
float s = float.Parse("10499.9705314636");

Now s has the value 10499.97.

Is there a way to save all the precision digits?

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
keerthee
  • 812
  • 4
  • 17
  • 39
  • Use `double` or `decimal`. Decimal will even preserver trailing zeros, while `double` simply offers better precision. If you are dealing with money, decimal is a must – Panagiotis Kanavos Feb 03 '15 at 14:09
  • Not using `float`, no. As [documented](https://msdn.microsoft.com/en-us/library/system.single), `float` has about 7 digits of precision. – Jon Skeet Feb 03 '15 at 14:09
  • Related: http://stackoverflow.com/questions/8509442/net-parse-round-float-values – TobiMcNamobi Feb 03 '15 at 14:12
  • Also note that `double` is faster for calculations but you *will* get rounding errors. – Panagiotis Kanavos Feb 03 '15 at 14:12
  • @PanagiotisKanavos, `Decimal` has more precision than `Double`.. or were you saying that `Double` offers more precision than `Float` in your remark? – Brett Caswell Feb 03 '15 at 14:32
  • Double has more precision than float. Decimal offers even better precision *and* preserves trailing zeros. – Panagiotis Kanavos Feb 03 '15 at 14:34
  • FYI, the closest IEEE 32 bit binary float to 10499.9705314636 is 10499.970703125, the closest 64 bit is 10499.970531463599400012753903865814208984375. If neither of those is close enough, go decimal. – Patricia Shanahan Feb 03 '15 at 16:54

3 Answers3

6

You may want to look into the difference between float, double, and decimal. Pay special attention to the difference between a binary floating point type, and a decimal floating point type. Decimal data type is likely what you're looking for here, as float doesn't have enough significant digits to store the number you're trying to parse accurately.

Community
  • 1
  • 1
joelmdev
  • 11,083
  • 10
  • 65
  • 89
3

Try using a data type with a higher precision than float, such as double. The remaining digits of your value are truncated due to the size of the data type float:

double s = double.Parse("10499.9705314636");
Console.WriteLine(s);

Prints:

10499.9705314636

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
2

You should try and use decimal instead.

HavelTheGreat
  • 3,299
  • 2
  • 15
  • 34