From 8edc3bfbaae23667f2383589f0f5f383cdf4b3d6 Mon Sep 17 00:00:00 2001 From: iaforek Date: Thu, 1 Jun 2017 00:24:21 +0100 Subject: [PATCH] BAEL-821 - How to round a number to n decimal places in Java (#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 --- core-java/pom.xml | 7 ++++ .../main/java/com/baeldung/maths/Round.java | 38 +++++++++++++++++++ .../java/com/baeldung/maths/RoundTest.java | 29 ++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 core-java/src/main/java/com/baeldung/maths/Round.java create mode 100644 core-java/src/test/java/com/baeldung/maths/RoundTest.java diff --git a/core-java/pom.xml b/core-java/pom.xml index bcec20b1e1..5c9bf06a57 100644 --- a/core-java/pom.xml +++ b/core-java/pom.xml @@ -45,6 +45,12 @@ commons-math3 ${commons-math3.version} + + + org.decimal4j + decimal4j + ${decimal4j.version} + org.bouncycastle @@ -369,6 +375,7 @@ 1.55 1.10 3.6.1 + 1.0.3 2.5 4.1 4.01 diff --git a/core-java/src/main/java/com/baeldung/maths/Round.java b/core-java/src/main/java/com/baeldung/maths/Round.java new file mode 100644 index 0000000000..3b30ab2c7a --- /dev/null +++ b/core-java/src/main/java/com/baeldung/maths/Round.java @@ -0,0 +1,38 @@ +package com.baeldung.maths; + +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.text.DecimalFormat; + +import org.apache.commons.math3.util.Precision; +import org.decimal4j.util.DoubleRounder; + +public class Round { + private static final double PI = 3.1415d; + + public static void main (String args[]) { + System.out.println("PI: " + PI); + System.out.printf("Value with 3 digits after decimal point %.3f %n", PI); + // OUTPUTS: Value with 3 digits after decimal point 3.142 + DecimalFormat df = new DecimalFormat("###.###"); + System.out.println(df.format(PI)); + System.out.println(round(PI, 3)); + System.out.println(roundOptional(PI, 3)); + System.out.println(Precision.round(PI, 3)); + System.out.println(DoubleRounder.round(PI, 3)); + } + + public static double round(double value, int places) { + if (places < 0) throw new IllegalArgumentException(); + + BigDecimal bd = new BigDecimal(value); + bd = bd.setScale(places, RoundingMode.HALF_UP); + return bd.doubleValue(); + } + + public static double roundOptional(double value, int places) { + double scale = Math.pow(10, places); + double rounded = Math.round(value * scale) / scale; + return rounded; + } +} diff --git a/core-java/src/test/java/com/baeldung/maths/RoundTest.java b/core-java/src/test/java/com/baeldung/maths/RoundTest.java new file mode 100644 index 0000000000..2621cfb65c --- /dev/null +++ b/core-java/src/test/java/com/baeldung/maths/RoundTest.java @@ -0,0 +1,29 @@ +package com.baeldung.maths; + +import org.apache.commons.math3.util.Precision; +import org.decimal4j.util.DoubleRounder; +import org.junit.Assert; +import org.junit.Test; + +public class RoundTest { + private double value = 2.03456d; + private int places = 2; + private double delta = 0.0d; + private double expected = 2.03d; + + @Test + public void givenDecimalNumber_whenRoundToNDecimalPlaces_thenGetExpectedResult() { + Assert.assertEquals(expected, Round.round(value, places), delta); + Assert.assertEquals(expected, Round.roundOptional(value, places), delta); + Assert.assertEquals(expected, Precision.round(value, places), delta); + Assert.assertEquals(expected, DoubleRounder.round(value, places), delta); + + places = 3; + expected = 2.035d; + + Assert.assertEquals(expected, Round.round(value, places), delta); + Assert.assertEquals(expected, Round.roundOptional(value, places), delta); + Assert.assertEquals(expected, Precision.round(value, places), delta); + Assert.assertEquals(expected, DoubleRounder.round(value, places), delta); + } +}