55

I have these two variables

double num = 540.512
double sum = 1978.8

Then I did this expression

double total = Math.round((num/ sum * 100) * 10) / 10;

but I end up with 27.0.

In fact I have many other variables and when I use them in the expression I always get a 0 in the tenth's place.

spongebob
  • 8,370
  • 15
  • 50
  • 83
user3344542
  • 551
  • 1
  • 4
  • 3
  • Possible duplicate of [How to round a number to n decimal places in Java](http://stackoverflow.com/questions/153724/how-to-round-a-number-to-n-decimal-places-in-java) – José Sep 24 '16 at 04:53

9 Answers9

113

Helpful method I created a while ago...

private static double round (double value, int precision) {
    int scale = (int) Math.pow(10, precision);
    return (double) Math.round(value * scale) / scale;
}
jpdymond
  • 1,517
  • 1
  • 8
  • 10
29

try this

for example

DecimalFormat df = new DecimalFormat("#.##");
df.format(55.544545);

output:

55.54
user3381763
  • 487
  • 3
  • 4
26
Double toBeTruncated = new Double("2.25");

Double truncatedDouble = new BigDecimal(toBeTruncated).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();

it will return 2.3

Minas Mina
  • 2,058
  • 3
  • 21
  • 35
Sankar
  • 1,272
  • 2
  • 19
  • 31
  • 7
    BTW, you probably don't want a variable name with `truncated` in it, since that has a different connotation than `round`. How about `toBeRounded` and `roundedDouble`? I mention this, because my first thought in seeing your answer was "Why is he truncating, when rounding was asked for?" – ToolmakerSteve Aug 28 '15 at 01:20
24

The Math.round method returns a long (or an int if you pass in a float), and Java's integer division is the culprit. Cast it back to a double, or use a double literal when dividing by 10. Either:

double total = (double) Math.round((num / sum * 100) * 10) / 10;

or

double total = Math.round((num / sum * 100) * 10) / 10.0;

Then you should get

27.3
rgettman
  • 176,041
  • 30
  • 275
  • 357
13

If you need this and similar operations more often, it may be more convenient to find the right library instead of implementing it yourself.

Here are one-liners solving your question from Apache Commons Math using Precision, Colt using Functions, and Weka using Utils:

double value = 540.512 / 1978.8 * 100;
// Apache commons math
double rounded1 = Precision.round(value, 1);
double rounded2 = Precision.round(value, 1, BigDecimal.ROUND_HALF_UP);
// Colt
double rounded3 = Functions.round(0.1).apply(value)
// Weka
double rounded4 = Utils.roundDouble(value, 1)

Maven dependencies:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-math3</artifactId>
    <version>3.5</version>
</dependency>

<dependency>
    <groupId>colt</groupId>
    <artifactId>colt</artifactId>
    <version>1.2.0</version>
</dependency>

<dependency>
    <groupId>nz.ac.waikato.cms.weka</groupId>
    <artifactId>weka-stable</artifactId>
    <version>3.6.12</version>
</dependency>
Mifeet
  • 12,949
  • 5
  • 60
  • 108
7
Double number = new Double("5.25");
Double tDouble = 
  new BigDecimal(number).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();

this will return 5.3

Youri
  • 442
  • 5
  • 15
sankar ganesh
  • 133
  • 1
  • 8
5

A neat alternative that is much more readable in my opinion, however, arguably a tad less efficient due to the conversions between double and String:

double num = 540.512;
double sum = 1978.8;

// NOTE: This does take care of rounding
String str = String.format("%.1f", (num/sum) * 100.0); 

If you want the answer as a double, you could of course convert it back:

double ans = Double.parseDouble(str);
Micah Stairs
  • 235
  • 2
  • 11
  • 3
    Conversion to and from String is much more than `a tad` less efficient. If the question really was about `display to one decimal digit` (and it might be, not clear), then you've got a great answer. But if that is not the desire, this isn't a good path. Stick to doing numeric operations on numbers, unless the desired result is a string. – ToolmakerSteve Aug 28 '15 at 01:24
5
DecimalFormat decimalFormat = new DecimalFormat(".#");
String result = decimalFormat.format(12.763);                // -->  12.7
Dan Alboteanu
  • 9,404
  • 1
  • 52
  • 40
4

Your method is right, all you have to do is add a .0 after both the tens and it will fix your problem!

double example = Math.round((187/35) * 10.0) / 10.0;

The output would be:

5.3