-1

So basically i want my float number price (eg:- 20.0) to be 20.00, with an additional zero. So what i tried to do below here is first to add an extra zero, which results in converting it to a string and then convert it back to float because my object type needs to store it as 20.00 as a Float.

Float lowest = productDetail1.getProductSummary().getPrice().getLowest();
String lowestPrice = String.format("$%.2f", lowest);
  try {
    Float floatVal = Float.valueOf(lowestPrice).floatValue();
    productDetail1.getProductSummary().getPrice().setLowest(floatVal);
  }catch (NumberFormatException e){
    LOGGER.error("Number Format Exception " + e.getMessage());
  }

So i debugged it, and i found out that the first 2 lines of codes work fine, but an error is thrown at line 3 :-

Float floatVal = Float.valueOf(lowestPrice).floatValue();

The error im getting is :- java.lang.numberformatexception for input string : "$10.00"

I hope someone can help me with this. A written code example will be helpful as a solution.

Ankit Beniwal
  • 509
  • 7
  • 18
  • There is a dollar sign in your `lowestPrice`. – Crude Code Oct 20 '20 at 07:04
  • 3
    why you need it to be .00 as float? it is just a display, you can convert it with `%.2f` at display time – Greedo Oct 20 '20 at 07:04
  • @Greedo I need to display this in a html, where im sending this as a model.attribute, so yeah i need to convert it to .00 in java itself – Yeshan Santhush Oct 20 '20 at 07:12
  • 2
    `20.0` and `20.00` are exactly the same `float` number. You can't have a `float` equal to `20.00` and not equal to `20.0`. Now, `"20.0"` and `"20.00"` are different string representations of the same number. – printf Oct 20 '20 at 07:21
  • Floating point has no precision (decimals after point) and are just an approximation, 1000*x where x is 0.1 will show an approximation error. And then there is that dollar character where you tripped over. Try BigDecimal. – Joop Eggen Oct 20 '20 at 07:26
  • @YeshanSanthush yes, but you don't need to save it in this way, you just need to format it just before to send it to HTML – Greedo Oct 20 '20 at 07:27
  • @YeshanSanthush See the duplicate links atop of your question, they should push you into the right direction. It is almost always better to work with Integer types, like `int` or `long` than with floating point types like `float` or `double` when working with currencies – Lino Oct 20 '20 at 07:34

1 Answers1

1

It is not possible to parse $ as part of the number. You need to remove that from the String before parsing it.
For example

Float floatVal = Float.valueOf(lowestPrice.substring(1)).floatValue();
Turamarth
  • 2,282
  • 4
  • 25
  • 31
  • ok so tried this, but still since i convert it back to float..the value becomes 20.0 again without being 20.00, u have any solution for that? @Turamarth – Yeshan Santhush Oct 20 '20 at 07:10
  • 2
    `20.0` and `20.00` are exactly the same `float` number. You can't have a `float` equal to `20.00` and not equal to `20.0`. Now, `"20.0"` and `"20.00"` are different string representations of the same number. – printf Oct 20 '20 at 07:21
  • yeah thnx, but i had to create a bean class and send the value which is "20.00" as a string. – Yeshan Santhush Oct 20 '20 at 11:54