5 Replies - 486 Views - Last Post: 07 October 2013 - 02:18 PM Rate Topic: -----

#1 cmurda  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 841
  • View blog
  • Posts: 2,576
  • 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).
Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2720
  • View blog
  • Posts: 11,447
  • 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);


Was This Post Helpful? 0
  • +
  • -

#4 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 841
  • View blog
  • Posts: 2,576
  • 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

Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2720
  • View blog
  • Posts: 11,447
  • 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
Was This Post Helpful? 1
  • +
  • -

#6 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 841
  • View blog
  • Posts: 2,576
  • 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. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1