This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

DecimalFormat average

 
Kevin Tysen
Ranch Hand
Posts: 255
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this in my program, (almost like this, anyway).


int cT = 17;
int ns = 4;
NumberFormat f = new DecimalFormat("0.0");
double ts = (double) cT;
double n = (double) ns;
writer.write(f.format(ts / n));


And when I run the program I think I should get 4.3, because 17/4 is 4.25, but I get 4.2. Why is that? What can I do about it?
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to the JavaDoc, DecimalFormat uses "half even rounding", which is described as...

Rounding mode to round towards the "nearest neighbor" unless both neighbors are equidistant, in which case, round towards the even neighbor. Behaves as for ROUND_HALF_UP if the digit to the left of the discarded fraction is odd; behaves as for ROUND_HALF_DOWN if it's even. Note that this is the rounding mode that minimizes cumulative error when applied repeatedly over a sequence of calculations.


Henry
 
Kevin Tysen
Ranch Hand
Posts: 255
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. But what is JavaDoc? I looked at the API for both NumberFormat and DecimalFormat, but I couldn't find the quote you quoted.

I assume one can use ROUND_HALF_UP like this?

myFormat = new NumberFormat("0.0");
myFormat.setRounding(NumberFormat.ROUND_HALF_UP);

Of course, it must be a different class. Well, if you can tell me how to look at JavaDoc, I can study it for myself.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, JavaDoc is the tool that produces the API documentation. Under DecimalFormat, the API documentation says...
Rounding

DecimalFormat provides rounding modes defined in RoundingMode for formatting. By default, it uses RoundingMode.HALF_EVEN.

When you click on RoundingMode.HALF_EVEN, you are taken to the definition Henry quoted from java.math.RoundingMode.

Using the setRoundingMode method in DecimalFormat will change this as desired...

myFormat.setRoundingMode(RoundingMode.HALF_UP);
 
Kevin Tysen
Ranch Hand
Posts: 255
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was looking at the API for Java 1.3 and that is why I couldn't find the quote. The API for DecimalFormat in Java 1.3 says nothing about rounding.

I have one computer with Java 1.3 and one with Java 1.5 and my program so far runs on both computers.

Java 1.5 has a note about rounding, but it refers to a field in the class BigDecimal. I think Java 1.3 has BigDecimal, too, so I will look carefully at the API for BigDecimal and see if I can use it to do what I want, or maybe link it somehow to NumberFormat or DecimalFormat.
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so I will look carefully at the API for BigDecimal and see if I can use it to do what I want,


Take a look at the setScale() method. With Java 1.3, that's probably the easiest way to get what you want.

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic