Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Java and postgres value of BigDecimal/numeric

I'm using BigDecimal type in Java project. I'm trying to save it using function with numeric field. Java and postgres have different value. E.g. Value of 56.97 from Java saves as 56.9699999999999989 in postgres. How can I prevent it?

like image 498
Bob Avatar asked Oct 14 '25 08:10

Bob


1 Answers

If you go to postgresql docs: https://www.postgresql.org/docs/current/datatype-numeric.html

I guess you are using real or double precision which are described as "variable-precision, inexact".

If you want it to be exact, let's say we are talking about money or similar, then you should use numeric or decimal types:

NUMERIC(precision, scale)

The precision is the total number of digits, while the scale is the number of digits in the fraction part.

For example, the number 1234.567 has a precision of seven and a scale of three.

like image 194
Buddy Christ Avatar answered Oct 17 '25 02:10

Buddy Christ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!