I'm working on java(swing) which is connected to database on H2. I've attached a screenshot of the JFrame, i'm entering values as a user and in database its storing some garbage value. (refer in the database screen shot, second column where empid=1300)
Asked
Active
Viewed 551 times
-1
-
Required Reading: [What Every Computer Scientist Should Know About Floating-Point Arithmetic](https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html) and [Is Floating Point Broken?](https://stackoverflow.com/q/588004/18157) – Jim Garrison Jun 23 '18 at 23:09
1 Answers
1
It's because they can't be accurately represented and you should use BigDecimal
in java and DECIMAL
in SQL. Refer here http://www.h2database.com/html/datatypes.html#decimal_type. It says:
DECIMAL type: Data type with fixed precision and scale. This data type is recommended for storing currency values.
DOUBLE type: A floating point number. Should not be used to represent currency values, because of rounding problems. If precision value is specified for FLOAT type name, it should be from 25 to 53.

Coder-Man
- 2,391
- 3
- 11
- 19