Formatting a BigDecimal Friday, February 03, 2006

Ok, this one is just me being stupid.
It took me slightly longer to figure out how to apply a format to a BigDecimal than it should have.

solution:
using java.math.BigDecimal and java.text.DecimalFormat.
BigDecimal bd = getIt();
DecimalFormat frmt = new DecimalFormat("$0.00");
String formatted = frmt.format(bd.doubleValue());


Feel free to make fun of me now.


<willCode4Beer/> posted at 10:58 AM

2 Comments:
Blogger Phy6 spewed forth...

Careful -- DecimalFormat uses ROUND_HALF_EVEN, whereas BigDecimal uses ROUND_HALF_UP by default. I'm currently searching for a way to change DecimalFormat to be able to use ROUND_HALF_UP.

16 August, 2007 15:02  
Blogger pRzemo spewed forth...

Notice that conversion from BigDecimal to double might loose some information and slightly modify the number, which might not be acceptable i.e. in financial applications.

24 September, 2007 05:57  

Post a Comment

<<< This way to the egress