Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Big float numbers weird results

In java I am using float to store the numbers. I chose the float format as I am working both with integers and double numbers, where the numbers are different, there can be big integers or big double numbers with different number of decimals. But when I insert these numbers into database, the wrong number is stored. For example:

float value = 0f; value = 67522665; System.out.println(value);

Printed: 6.7522664E7 and it is stored in the database as 67522664 not as 67522665

like image 867
user1574866 Avatar asked Jun 01 '26 06:06

user1574866


1 Answers

Floating point numbers have limited resolution — roughly 7 significant digits. You are seeing round-off error. You can use a double for more resolution or, for exact arithmetic, use BigDecimal.

Suggested reading: What Every Computer Scientist Should Know About Floating-Point Arithmetic

like image 132
Ted Hopp Avatar answered Jun 03 '26 19:06

Ted Hopp