[JAVA] Pay attention to the boundary check of the input value when using the float type

At first

I just happened to find it on float, and I think the same thing will happen if you check the value on double.

Test code

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"));
 
    }

}

Execution result

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

URL for checking operation on the Web

https://paiza.io/projects/KMTyqbyvAG1M_8KslEVWiQ

Summary

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

Pay attention to the boundary check of the input value when using the float type
How to display 0 on the left side of the standard input value
[Rails] Talk about paying attention to the return value of where
When you want to check whether the contents of a property can be converted to a specific type
What I thought when passing the user input value to the Service class
Android development, how to check null in the value of JSON object
How to display the result of form input
Speed comparison when the value side of Hash wants to retrieve all with array
How to get the class name of the argument of LoggerFactory.getLogger when using SLF4J in Java
Spring 4.2 vs 4.3: Difference in bind value to Collection type when submitting check box empty
Sample code to assign a value in a property file to a field of the expected type
When the hover of Eclipse is hard to see
[Java] How to get the maximum value of HashMap
Things to be aware of when using devise's lockable
Rails6: Input the initial data of ActionText using seed
The first step to using Xib instead of StoryBoard
When using the constructor of Java's Date class, the date advances by 1900.
Command to check the number and status of Java threads
Check the status of Java application without using monitoring tool
[Java] To know the type information of type parameters at runtime
How to change the setting value of Springboot Hikari CP
[Swift] Get the timing when the value of textField is changed
When you want to change the MySQL password of docker-compose
The story of pushing Java to Heroku using the BitBucket pipeline
How to check the extension and size of uploaded files
[Apache Tomcat] The story of using Apache OpenWebBeans to enable CDI
How to check the database of apps deployed on Heroku
[Rails] How to convert the URI of the image sent by http to https when using Twitter API
Know the convenience of Docker (-compose) now (information list that I referred to when using it)
I want to make the frame of the text box red when there is an input error
Method definition location Summary of how to check When defined in the project and Rails / Gem