0

I have a double that I want to keep only 3 decimal places but without applying any rounding at all.
E.g. 92.36699 should be 92.366
I tried the following:

DecimalFormat nf= new DecimalFormat("#0.000");  
String number = nf.format(originalNumber);  

But this results in 92.367
How can I do what I need?

Jim
  • 18,826
  • 34
  • 135
  • 254

1 Answers1

4

This isn't "no rounding", it's DOWN rounding. Simply set the roundingMode.

DecimalFormat nf = new DecimalFormat("#0.000");  
nf.setRoundingMode(RoundingMode.DOWN);
String number = nf.format(originalNumber);  

Note the difference between FLOOR and DOWN - only relevant for negative numbers. FLOOR rounds towards negative infinity therefore -92.36699 would become "-92.367".

Boris the Spider
  • 59,842
  • 6
  • 106
  • 166