views:

2240

answers:

9

How do I round a double to 5 decimal places, without using DecimalFormat?

+2  A: 

If you are okay with external libraries, you can have a look at microfloat, specifically MicroDouble.toString(double d, int length).

Zed
A: 
double dbl = 12.345678;
int ix = (int)(dbl * 100000.0); // scale it
double dbl2 = ((double)ix)/100000.0;
System.out.println("dbl=" + dbl + ", dbl2=" + dbl2); //dbl=12.345678, dbl2=12.34567

Thats the way, without any lib.

Nadir SOUALEM
This is truncation, not rounding.
UncleO
to round, add 0.5
Thorbjørn Ravn Andersen
@Thorbjørn, I don't that is a good idea for numbers that are already integers. For instance round(1.00) -> 1 should be the actual result, and not round(1.00) -> 2, because the latter would do round(1.00 + .5).
Vineet Reynolds
@Vineet: Don't round at the end, truncate. See my answer.
Thomas Padron-McCarthy
@Nadir: Will the code still work in case of negative numbers?
Kevin Boyd
+5  A: 

You can round to the fifth decimal place by making it the first decimal place by multiplying your number. Then do normal rounding, and make it the fifth decimal place again.

Let's say the value to round is a double named x:

double factor = 1e5; // = 1 * 10^5 = 100000.
double result = Math.round(x * factor) / factor;

If you want to round to 6 decimal places, let factor be 1e6, and so on.

Joren
-1: can't believe this got voted up that much, as this isn't really rounding at all. See Jon's answer for why.
Michael Borgwardt
I don't see the problem. It was asked how to round a `double` to five decimal places, and this is the best you're going to get. Obviously you should not actually use a `double` if you're representing decimal values, but that wasn't the question.
Joren
+11  A: 

Whatever you do, if you end up with a double value it's unlikely to be exactly 5 decimal places. That just isn't the way binary floating point arithmetic works. The best you'll do is "the double value closest to the original value rounded to 5 decimal places". If you were to print out the exact value of that double, it would still probably have more than 5 decimal places.

If you really want exact decimal values, you should use BigDecimal.

Jon Skeet
A: 
public static double roundNumber(double num, int dec) {
     return Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
}
Niger
+1  A: 

Multiply by 100000. Add 0.5. Truncate to integer. Then divide by 100000.

Code:

double original = 17.77777777;
int factor = 100000;
int scaled_and_rounded = (int)(original * factor + 0.5);
double rounded = (double)scaled_and_rounded / factor;
Thomas Padron-McCarthy
+2  A: 

Try the following

double value = Double.valueOf(String.format(Locale.US, "%1$.5f", 5.565858845));

System.out.println(value); // prints 5.56586

value = Double.valueOf(String.format(Locale.US, "%1$.5f", 5.56585258));

System.out.println(value); // prints 5.56585

Or if you want minimal amount of code

Use import static

import static java.lang.Double.valueOf;
import static java.util.Locale.US;
import static java.lang.String.format;

And

double value = valueOf(format(US, "%1$.5f", 5.56585258));

regards,

Arthur Ronald F D Garcia
+2  A: 
DecimalFormat roundFormatter = new DecimalFormat("########0.00000");

public Double round(Double d)
    {
        return Double.parseDouble(roundFormatter.format(d));
    }
Milhous