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?
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.
I assume one can use ROUND_HALF_UP like this?
myFormat = new NumberFormat("0.0");
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.
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...
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.