Skip to content

Commit 8edc3bf

Browse files
iaforekpedja4
authored andcommitted
BAEL-821 - How to round a number to n decimal places in Java (eugenp#1953)
* Code for Dependency Injection Article. * Added Java based configuration. Downloaded formatter.xml and reformatted all changed files. Manually changed tab into 4 spaces in XML configuration files. * BAEL-434 - Spring Roo project files generated by Spring Roo. No formatting applied. Added POM, java and resources folders. * Moved project from roo to spring-roo folder. * BAEL-838 Initial code showing how to remove last char - helper class and tests. * BAEL-838 Corrected Helper class and associated empty string test case. Added StringUtils.substing tests. * BAEL-838 Refromatted code using formatter.xml. Added Assert.assertEquals import. Renamed test to follow convention. Reordered tests. * BAEL-838 - Added regex method and updated tests. * BAEL-838 Added new line examples. * BAEL-838 Renamed RemoveLastChar class to StringHelper and added Java8 examples. Refactord code. * BAEL-838 Changed method names * BAEL-838 Tiny change to keep code consistant. Return null or empty. * BAEL-838 Removed unresolved conflict. * BAEL-821 New class that shows different rounding techniques. Updated POM. * BAEL-821 - Added unit test for different round methods. * BAEL-821 Changed test method name to follow the convention
1 parent 0b108e3 commit 8edc3bf

3 files changed

Lines changed: 74 additions & 0 deletions

File tree

core-java/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@
4545
<artifactId>commons-math3</artifactId>
4646
<version>${commons-math3.version}</version>
4747
</dependency>
48+
49+
<dependency>
50+
<groupId>org.decimal4j</groupId>
51+
<artifactId>decimal4j</artifactId>
52+
<version>${decimal4j.version}</version>
53+
</dependency>
4854

4955
<dependency>
5056
<groupId>org.bouncycastle</groupId>
@@ -369,6 +375,7 @@
369375
<bouncycastle.version>1.55</bouncycastle.version>
370376
<commons-codec.version>1.10</commons-codec.version>
371377
<commons-math3.version>3.6.1</commons-math3.version>
378+
<decimal4j.version>1.0.3</decimal4j.version>
372379
<commons-io.version>2.5</commons-io.version>
373380
<commons-collections4.version>4.1</commons-collections4.version>
374381
<collections-generic.version>4.01</collections-generic.version>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.maths;
2+
3+
import java.math.BigDecimal;
4+
import java.math.RoundingMode;
5+
import java.text.DecimalFormat;
6+
7+
import org.apache.commons.math3.util.Precision;
8+
import org.decimal4j.util.DoubleRounder;
9+
10+
public class Round {
11+
private static final double PI = 3.1415d;
12+
13+
public static void main (String args[]) {
14+
System.out.println("PI: " + PI);
15+
System.out.printf("Value with 3 digits after decimal point %.3f %n", PI);
16+
// OUTPUTS: Value with 3 digits after decimal point 3.142
17+
DecimalFormat df = new DecimalFormat("###.###");
18+
System.out.println(df.format(PI));
19+
System.out.println(round(PI, 3));
20+
System.out.println(roundOptional(PI, 3));
21+
System.out.println(Precision.round(PI, 3));
22+
System.out.println(DoubleRounder.round(PI, 3));
23+
}
24+
25+
public static double round(double value, int places) {
26+
if (places < 0) throw new IllegalArgumentException();
27+
28+
BigDecimal bd = new BigDecimal(value);
29+
bd = bd.setScale(places, RoundingMode.HALF_UP);
30+
return bd.doubleValue();
31+
}
32+
33+
public static double roundOptional(double value, int places) {
34+
double scale = Math.pow(10, places);
35+
double rounded = Math.round(value * scale) / scale;
36+
return rounded;
37+
}
38+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.baeldung.maths;
2+
3+
import org.apache.commons.math3.util.Precision;
4+
import org.decimal4j.util.DoubleRounder;
5+
import org.junit.Assert;
6+
import org.junit.Test;
7+
8+
public class RoundTest {
9+
private double value = 2.03456d;
10+
private int places = 2;
11+
private double delta = 0.0d;
12+
private double expected = 2.03d;
13+
14+
@Test
15+
public void givenDecimalNumber_whenRoundToNDecimalPlaces_thenGetExpectedResult() {
16+
Assert.assertEquals(expected, Round.round(value, places), delta);
17+
Assert.assertEquals(expected, Round.roundOptional(value, places), delta);
18+
Assert.assertEquals(expected, Precision.round(value, places), delta);
19+
Assert.assertEquals(expected, DoubleRounder.round(value, places), delta);
20+
21+
places = 3;
22+
expected = 2.035d;
23+
24+
Assert.assertEquals(expected, Round.round(value, places), delta);
25+
Assert.assertEquals(expected, Round.roundOptional(value, places), delta);
26+
Assert.assertEquals(expected, Precision.round(value, places), delta);
27+
Assert.assertEquals(expected, DoubleRounder.round(value, places), delta);
28+
}
29+
}

0 commit comments

Comments
 (0)