How to count the number of digits of a floating number?

Float has some… annoying properties when it’s about rounding. Try the following:

  println(1234.45678);

it should return 1234.45678 in the console but it returns 1234.4568 instead. Some operations can cause it to randomly round and when multiplying with numbers >1 java might go crazy and add some junk-data. This is exactly what happened in your case together with a few other errors.

Another thing is: float only can have a maximum of 8 digits in total. For more digits try double. When defining a double don’t forget to add an d at the end of you number e.g.

10.1234567890234d
1 Like