Why does float have a trailing .0 when it is an integer?

Please read this question carefully because I am not asking how easy it is to get rid of trailing zeros.

What am I asking why 123d became "123.0"?

64-bit IEEE floats can accurately represent integers from 0 to 2 ^ 52, so this is not a loss of precision, but a solution when implementing Double.toString ().

My question is why did they make this decision the same way they did? Why didn't they just print 123?

+2


source to share


3 answers


Among other things, this leads to clarity of presentation - users are often confused when what appears to be an integer value suddenly has a long trail of digits after the decimal point after a simple arithmetic operation.



+8


source


So it would be easier to see that it was, in fact, a double and not an int I guess :).



+5


source


toString is a very special method. This does not mean "custom display string" by any stretch of the imagination (although this is often the same as the String toString method)

In fact, developers can instantly see what an object is (primarily as debug output).

For anything to be printed by the user, it must be displayed through the formatter anyway to ensure the correct maximum size, number of decimal points and decimal indicator (. Or,).

In Java code, 0.0 is automatically appended to the double, so it makes sense that double will print as 0.0 - that means "it's a double." I'm a little surprised that it doesn't end with "f" or "d", but it will actually be really annoying because toString for int is really very useful and constantly following it with "i" would be pissed off all off.

0


source







All Articles