What I need : I need to store a Float with two decimals in java (11)
What I did : I used the BigDecimal class with no problems, the scale is as expected even when the decimal portion is .00
What is my problem : When I convert the BigDecimal to a float with .floatValue() it removes the trailing 0 if the decimal portion is .00
A few examples
- 2.45 (Big dec) gets converted to 2.45 as a Float
- 2.00 (Big dec) gets converted to 2.0 as a Float ==> I want 2.00
Any solution ? Thanks !
EDIT : Outputting the float as a string is not what I want, I need to store the variable as a Float
I checked quickly DecimalFormat but at the moment I only found methods that format it as a string.
EDIT2 : I am working on a REST api, I want to return a JSON which has to output a price as a Float (else I would have returned it as a big decimal) I want to return the price with 2 decimals every time. It works in all cases except when the decimal portion is .00 (.01 to .99 works fine) as explained above. The Float type in the JSON is a constraint that I cannot change.