-1
float.Parse("534818068")

returns: 534818080

I understand that there are many complications with float and decimal values. But maybe someone could explain this behaviour to me.

Thanks!

Hannes
  • 426
  • 3
  • 12
  • `float` tends to single-precision, it only supports less than 10-digit floating-point notation. What I get is `5.348181E+08` which actually rounds up to 7 digits. – Tetsuya Yamamoto Nov 06 '17 at 09:05
  • "I understand that there are many complications with float and decimal values" - what are the complications that you understand? – Enigmativity Nov 06 '17 at 09:44

2 Answers2

1

Floating point numbers have a relative precision, i.e. something like 7 or 8 digits. So only the first 7 or 8 digits are correct, independent of the actual total size of the number.

J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142
0

Floating point numbers are stored internally using the IEEE 754 standard (a sign, a biased exponent and a fraction).

float numbers are stored with a 32 bits representation, which means they will have a precision of 7 digits. On the other hand, double are stored with a 64 bits representation, thus having 15-16 digits (source).

Which is why you shouldn't usually compare floats for equality for instance.

yorah
  • 2,653
  • 14
  • 24
  • That doesn't (in itself) explain what the OP's seeing though. (It would be fine for `double`, for example.) – Jon Skeet Nov 06 '17 at 09:12
  • True. Because double has a larger precision (stored with 64 bits vs 32 bits for float). However, I think the explanation of what the OP is seeing lies in how the numbers are represented internally, thus my answer. – yorah Nov 06 '17 at 09:18
  • Not really - because without mentioning precision, it doesn't answer it at all. Integers can always be represented precisely in a binary floating point type with a large enough mantissa to handle that integer. Basically, you're only providing half the relevant information at the moment. It's a fine start, but it doesn't answer the question *yet*. – Jon Skeet Nov 06 '17 at 09:20