spring - Storing trailing zeroes in database with JPA and Oracle -
i need store prices in database. i'm using jpa, i've got model this:
@entity @table(name="products") public class product { @id @generatedvalue(strategy=generationtype.identity) private long id; @column(name="price") private float price; }
the problem when fill price form inputs values "4.20", on oracle database "4.2", losing trailing zero.
how can solve problem?
edit: since i'm using jpa, have avoid writing queries in native oracle dialect. store products (with prices) write em.persist(product)
, em entitymanager
you can't solve it, , i'd suggest it's not problem. that's how oracle stores number
values: least precision needed. if entered price 4.00
stored 4
.
note java won't default 2 decimals either when displaying value. need specify number of decimal places.
and, if possible, i'd use bigdecimal
instead of float
price. float
type isn't precise. alternative use 1 of integer types (int
or long
) , manage decimal places yourself.
to format price display in java code, use string.format("%.2f", price)
.
to format price in oracle query, use to_char
:
select to_char(price, '999999990.00'), etc.
choose number of placeholders that's best you, , note oracle put space @ beginning of result. it's meant hold minus sign if number negative, zero/positive it's space. rid of space use fm
modifier in to_char
:
select to_char(price, 'fm999999990.00'), etc.
Comments
Post a Comment