Mickey Patel
Mickey Patel

Reputation: 321

How to store exact precision value in java and hibernate?

User input is: 44.4

While storing into database its converting to: 44.40000153

Database Data Type is : float and Length is : 20,8

Here I want to store exactly 44.4

I don't want to change database design.

Upvotes: 0

Views: 947

Answers (2)

Sagar Gangwal
Sagar Gangwal

Reputation: 7937

field float(20,1)

You can try using above change.

Make it 20,1

I had tried same as below

create table abc(field float(20,1));
insert into abc value(44.4);
select * from abc

Or, if you are unable to change database then go with below query

select cast(field as decimal(10,1)) from abc;

Here I had used CAST

And it shows me your expected result.

Upvotes: 1

Thilo
Thilo

Reputation: 262484

Don't use float then. Use number(10,5) (or however many digits you need) in the database and BigDecimal in Java.

BigDecimal gives you an unlimited number of digits, but in the database there are limits to what can be stored. Usually something like 38 digits (i.e. usually enough).

Upvotes: 3

Related Questions