0

I am trying to Format my double value to exact 2 decimal places and it seems to working fine, here is the code i am trying

final NumberFormat df = DecimalFormat.getInstance();
df.setMinimumFractionDigits(2);
df.setMaximumFractionDigits(2);
df.setRoundingMode(RoundingMode.DOWN);
df.format(value)

Till now everything is working, but i need to return double value as it is being used for other calculations and i tried

Double.parseDouble(df.format(value))

with big decimal like

BigDecimal price = new BigDecimal(df.format(number));

but it is not working as expected like 18.50 is being converted as 18.5

Though this is not an issue with calculations but i need to show amount on the UI where i have to show exactly up to 2 decimal places.

Is there any was i can handle it in java class or i have to take care in JSP with JSTL

Umesh Awasthi
  • 22,642
  • 37
  • 122
  • 198
  • I do not get it: what are you trying to print that does not print correctly? – Jean Logeart Apr 10 '13 at 07:30
  • @Vakh: i told that 18.50 is being returned as 18.5 when converted to double in end – Umesh Awasthi Apr 10 '13 at 07:31
  • Is [this](http://www.tutorialspoint.com/jsp/jstl_format_formatnumber_tag.htm) what you're looking for? Or you could pass a String to your JSP. It's hard to tell what you're trying to do and/or what you're expecting. – jahroy Apr 10 '13 at 07:33
  • @jahroy: i am aware about JSTL approach, but that means adding the tag to every place where need to show the price, i want this to handle in java, sending string to jsp is like changing your existing data class and introduce additional field – Umesh Awasthi Apr 10 '13 at 07:35
  • 2
    I'm confused. Java does not store numbers in its internal formats. When you say "*it is not working as expected like 18.50 is being converted as 18.5*" you're referring to their `String` representation. And whether you keep trailing zeroes or not depends on the conversion method. If you do not want to deal with the presentation layer in your UI then you need to pass already formatted **strings**. – PM 77-1 Apr 10 '13 at 07:46
  • @PM77-1 - You and I have the same interpretation of this question. – jahroy Apr 11 '13 at 04:42

1 Answers1

2

This is what BigDecimal is made for!

BigDecimal number = new BigDecimal(123.456);
// set 2 fraction digits
// Note that setScale() does not change the original, 
// but returns a new BigDecimal.
number = number.setScale(2, RoundingMode.DOWN);
// get string representation
String text = number.toPlainString();
// get double value
double dbl = number.doubleValue();

And use BigDecimal for other calculations as well if you can.

drzymala
  • 1,846
  • 19
  • 23
  • Thanks for the input but `number.doubleValue()` for 18.50 will return 18.5 which means for the UI display i need to have another input field – Umesh Awasthi Apr 10 '13 at 07:46
  • 1
    @Umesh Why do you need another input field? A double will never ever be 18.50 Use a string representation instead. – drzymala Apr 10 '13 at 08:00
  • 1
    @Umesh, I agree with @martini, use _string_ to respresent. You can use `DecimalFormat.applyPattern("0.00")` which is more simple. – hiway Apr 10 '13 at 08:12
  • 1
    @Hiway You have not used BigDecimals have you? toPlainString() is *the* simplest. – drzymala Apr 10 '13 at 08:16