Reputation: 571
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.
Upvotes: 57
Views: 217706
Reputation: 1
This is an improvement on @jpydymond's answer, as it corrects for the problem where the internal value of sub-decimal '.xxxx5' can really be '.xxxx499999...'. That can cause his 'round(123.335,2)' to return 123.33 instead of the desired 123.34. The snippet fixes that and also constrains to the limit of 0...9 decimal places due to 64-bit precision limits.
public static double round (double value, int decimalPlaces) {
if(decimalPlaces < 0 || decimalPlaces > 9) {
throw new IllegalArgumentException("The specified decimalPlaces must be between 0 and 9 (inclusive).");
}
int scale = (int) Math.pow(10, decimalPlaces);
double scaledUp = value * scale;
double dec = scaledUp % 1d;
double fixedDec = Math.round(dec*10)/10.;
double newValue = scaledUp+fixedDec;
return (double) Math.round( newValue )/scale;
}
Sample output:
round(265.335,0) = 266.0
round(265.335,2) = 265.34
round(265.3335,3) = 265.334
round(265.3333335,6) = 265.333334
round(265.33333335,7) = 265.3333334
round(265.333333335,8) = 265.33333334
round(265.3333333335,9) = 265.333333334
round(1265.3333333335,9) = 1265.333333334
round(51265.3333333335,9) = 51265.333333334
round(251265.3333333335,9) = 251265.333333334
round(100251265.3333333335,9) = 1.0025126533333333E8
round(0.1,0) = 0.0
round(0.1,5) = 0.1
round(0.1,7) = 0.1
round(0.1,9) = 0.1
round(16.45,1) = 16.5
I hope this is helpful.
Upvotes: 0
Reputation: 133
Double number = new Double("5.25");
Double tDouble =
new BigDecimal(number).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
this will return 5.3
Upvotes: 7
Reputation: 1292
Double toBeTruncated = new Double("2.25");
Double truncatedDouble = new BigDecimal(toBeTruncated).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
it will return 2.3
Upvotes: 26
Reputation: 10252
DecimalFormat decimalFormat = new DecimalFormat(".#");
String result = decimalFormat.format(12.763); // --> 12.7
Upvotes: 4
Reputation:
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
Upvotes: 6
Reputation: 13648
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>
Upvotes: 13
Reputation: 235
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);
Upvotes: 5
Reputation: 505
try this
for example
DecimalFormat df = new DecimalFormat("#.##");
df.format(55.544545);
output:
55.54
Upvotes: 30
Reputation: 1612
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;
}
Upvotes: 120
Reputation: 178313
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
Upvotes: 24