Take the 2-minute tour ×

How do I prevent String.format("%.2f", doubleValue); from rounding off (round half up algorithm) instead of just truncating it?


doubleValue = 123.459

after formatting,

doubleValue = 123.46

I just want to discard the last digit,


I know there are other ways to do this, I just want to know if this is possible using the String.format.

share|improve this question
add comment

4 Answers

up vote 5 down vote accepted

You can always set the rounding mode:


and then use String.Format() HALF_EVEN is used by default, but you can change it to CEILING

another no so flexible approach will be (but this is not what you asked about):

share|improve this answer
add comment
doubleValue = 123.459
doubleValue = Math.ceil(doubleValue*100)/100;
share|improve this answer
If you want to truncate, don't you want to use floor for positive values, and ceil for negative ones? –  Rob Nov 23 '12 at 14:25
add comment

Looks like the answer is a big fat NO. http://java.sun.com/javase/6/docs/api/java/util/Formatter.html#dndec "then the value will be rounded using the round half up algorithm"

I find it odd they'd do that, since NumberFormatter allows you to set RoundingMode. But as you say, there's other ways to do it. The obviously easiest being subtract .005 from your value first.

share|improve this answer
add comment

Or format it to %.3f and truncate.

share|improve this answer
add comment

Your Answer


