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
This commit is contained in:
29
core-java/src/test/java/com/baeldung/maths/RoundTest.java
Normal file
29
core-java/src/test/java/com/baeldung/maths/RoundTest.java
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user