I just happened to find it on float, and I think the same thing will happen if you check the value on double.
public class Main {
public static void main(String[] args) throws Exception {
System.out.println("Float.parseFloat(999999967) --> " + Float.parseFloat("999999967"));
System.out.println("Float.parseFloat(999999968) --> " + Float.parseFloat("999999968"));
System.out.println("Float.parseFloat(999999969) --> " + Float.parseFloat("999999969"));
System.out.println("Float.parseFloat(999999999) --> " + Float.parseFloat("999999999"));
System.out.println("Float.parseFloat(1000000000) --> " + Float.parseFloat("1000000000"));
System.out.println("Float.parseFloat(1000000001) --> " + Float.parseFloat("1000000001"));
System.out.println("Float.parseFloat(1000000010) --> " + Float.parseFloat("1000000010"));
System.out.println("Float.parseFloat(1000000020) --> " + Float.parseFloat("1000000020"));
System.out.println("Float.parseFloat(1000000030) --> " + Float.parseFloat("1000000030"));
System.out.println("Float.parseFloat(1000000031) --> " + Float.parseFloat("1000000031"));
System.out.println("Float.parseFloat(1000000032) --> " + Float.parseFloat("1000000032"));
System.out.println("Float.parseFloat(1000000033) --> " + Float.parseFloat("1000000033"));
System.out.println("Float.parseFloat(1000000034) --> " + Float.parseFloat("1000000034"));
System.out.println("Float.parseFloat(1000000035) --> " + Float.parseFloat("1000000035"));
System.out.println("Float.parseFloat(1000000040) --> " + Float.parseFloat("1000000040"));
}
}
Float.parseFloat(999999967) --> 9.9999994E8
Float.parseFloat(999999968) --> 1.0E9
Float.parseFloat(999999969) --> 1.0E9
Float.parseFloat(999999999) --> 1.0E9
Float.parseFloat(1000000000) --> 1.0E9
Float.parseFloat(1000000001) --> 1.0E9
Float.parseFloat(1000000010) --> 1.0E9
Float.parseFloat(1000000020) --> 1.0E9
Float.parseFloat(1000000030) --> 1.0E9
Float.parseFloat(1000000031) --> 1.0E9
Float.parseFloat(1000000032) --> 1.0E9
Float.parseFloat(1000000033) --> 1.00000006E9
Float.parseFloat(1000000034) --> 1.00000006E9
Float.parseFloat(1000000035) --> 1.00000006E9
Float.parseFloat(1000000040) --> 1.00000006E9
https://paiza.io/projects/KMTyqbyvAG1M_8KslEVWiQ
Converting 999,999,999 and 1,000,000,000 to float will result in the same value. Not only that, 999,999,968 to 1,000,000,032 are considered to be the same value.
It was an example that you should be careful when performing a check such as wanting to make an error if you enter a value larger than 999,999,999 (= 1,000,000,000). In this example, there are two choices, double or BigDecimal, and it is preferable to use BigDecimal if possible.
Recommended Posts