# How to use DecimalFormat to round to two decimal places?

Page 1 of 1

## 5 Replies - 808 Views - Last Post: 07 October 2013 - 02:18 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=330916&amp;s=033327ca6bfcfb21e82f3df9501718ba&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 cmurda

Reputation: 0
• Posts: 8
• Joined: 03-October 13

# How to use DecimalFormat to round to two decimal places?

Posted 06 October 2013 - 05:26 PM

I am having trouble using decimal format to round my answers to two decimal places.

DecimalFormat df = new DecimalFormat("#.##");
println("You owe: \$" + df.format(taxableIncome));

I thought that would work but it doesn't seem to, any pointers would be appreciated.
Is This A Good Question/Topic? 0

## Replies To: How to use DecimalFormat to round to two decimal places?

### #2 farrell2k

• D.I.C Lover

Reputation: 875
• Posts: 2,706
• Joined: 29-July 11

## Re: How to use DecimalFormat to round to two decimal places?

Posted 06 October 2013 - 05:53 PM

Just call setMaximumFractionDigits(2).

### #3 g00se

• D.I.C Lover

Reputation: 3064
• Posts: 13,139
• Joined: 20-September 08

## Re: How to use DecimalFormat to round to two decimal places?

Posted 07 October 2013 - 01:31 PM

```System.out.printf("You owe: \$%.2f%n", taxableIncome);

```

### #4 farrell2k

• D.I.C Lover

Reputation: 875
• Posts: 2,706
• Joined: 29-July 11

## Re: How to use DecimalFormat to round to two decimal places?

Posted 07 October 2013 - 01:43 PM

I don't think that handles proper rounding for financial applications, g00se. Maybe that's irrelevant to the OP's situation, however. />

This post has been edited by farrell2k: 07 October 2013 - 01:44 PM

### #5 g00se

• D.I.C Lover

Reputation: 3064
• Posts: 13,139
• Joined: 20-September 08

## Re: How to use DecimalFormat to round to two decimal places?

Posted 07 October 2013 - 01:51 PM

Well that depends what you mean by 'proper'. It's regular rounding

### #6 farrell2k

• D.I.C Lover

Reputation: 875
• Posts: 2,706
• Joined: 29-July 11

## Re: How to use DecimalFormat to round to two decimal places?

Posted 07 October 2013 - 02:18 PM

I mean half-up. I didn't know that did any rounding. That's interesting. It does do rounding half-up. I learned something today. Something I should have known already.