Java – why change the value from float to double?
•
Java
I've been trying to find out why, but I can't
Look at the following example
float f; f = 125.32f; System.out.println("value of f = " + f); double d = (double) 125.32f; System.out.println("value of d = " + d);
This is the output:
f = 125.32
Value d = 125.3199996948242
Solution
>When you convert a floating point number to double, you do not lose information Each floating-point number can be expressed as a double. > On the other hand, system out. Println prints decimal expressions that are not the exact value of a number Accurate decimal representation may require up to about 760 decimal digits Instead, system out. Println prints the exact decimal number that allows the decimal representation to be parsed into the original floating point number or double number There are more doubles, so when printing one, system out. Println needs to print more numbers to make the representation clear
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码