Untangledup 0 Newbie Poster

Greetings, I'm aware of the incorrect rouding that NumberFormat does in Java. We are working with dollars and cents but multiplying the amounts against an "area factor" with four or five decimal places. As a result, the dollar amounts, when returned, come back a penny off due to the rounding.

I was working with BigDecimal but don't know enough about it. Can anyone suggest some code to help resolve this rounding issue? Here is the original working code (but does not do the rounding correctly):

<code>
private String formatCurrencyValue( Float f )
{
try
{
String out = "0";
NumberFormat nf = NumberFormat.getInstance();
nf.setMinimumFractionDigits( 2 );
nf.setMaximumFractionDigits( 2 );
if ( f != null )
{
out = nf.format( f.doubleValue() );
return out;
}
else
{
return "0";
}
}
catch ( Exception e )
{
}
}
</code>

Many thanks,
Jim

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.