-3

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

  1. 2.45 (Big dec) gets converted to 2.45 as a Float
  2. 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.

SightBack
  • 71
  • 1
  • 7
  • use DecimalFormat formatter = new DecimalFormat("0.00") formatter.format(number); – Soni Sep 19 '20 at 16:18
  • Nope because I need to store the value, I tried to do Float.valueOf(String.format("%.2f",number).toString()) and it returned back as number.0 instead of number.00 as desired – SightBack Sep 19 '20 at 16:28
  • You need to explain why you think you need to store a float with two decimals, because what you're asking for doesn't actually make sense. But tell us why you think you need what you're asking for and maybe we can send you in the right direction. – pamphlet Sep 19 '20 at 16:43
  • OK, so this is a question about the formatting of JSON. I can't help you with that, but if you phrased the question "How to ensure floats are always emitted with two decimals in JSON?" with appropriate tags (or start with a search of that question), you'll probably get further. Hope this helps you out. – pamphlet Sep 19 '20 at 16:51
  • 1
    How are you creating the Json? You should be able to use a BigDecimal, and convert that to JSON without ever having to deal with a float. – matt Sep 19 '20 at 16:53
  • 2
    *"it removes the trailing 0"* No it doesn't, the zero is still there. A floating point value (`float` or `double`) doesn't store a "number of decimals". `3.00` and `3.0` is the **same numeric value**, and the floats store *numeric* values. The two decimals is a result of **formatting** the numeric value as text. Even in JSON, `3.00` and `3.0` is the same, however `"3.00"` is different from `"3.0"`. If the code reading the JSON doesn't know the difference between `3.0` and `"3.0"`, then *that* code is flawed and needs to be fixed. – Andreas Sep 19 '20 at 16:58
  • I see, thank you, I thought the problem was in the float conversion. – SightBack Sep 19 '20 at 17:01
  • *"The Float type in the JSON is a constraint that I cannot change."* --- Then you cannot change the "look" of the JSON. Besides, the JSON is correct, there is nothing wrong with it. – Andreas Sep 19 '20 at 17:01

1 Answers1

1

You can't store a float with two decimals. It's not how floats work. They're a binary representation of a number, not a decimal one.

You can format a float with two decimals.

pamphlet
  • 2,054
  • 1
  • 17
  • 27