Add Format Number implementation and tests
This commit is contained in:
parent
e689beb984
commit
dcb9afaaef
|
@ -0,0 +1,63 @@
|
|||
package com.baeldung.formatNumber;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.text.DecimalFormat;
|
||||
import java.text.NumberFormat;
|
||||
import java.util.Locale;
|
||||
|
||||
public class FormatNumber {
|
||||
private static final double D = 4.2352989244d;
|
||||
private static final double F = 8.6994540927d;
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(D + " with Big Decimal (2 places) is " + withBigDecimal(D, 2));
|
||||
System.out.println(D + " with Big Decimal (3 places) is " + withBigDecimal(D, 3));
|
||||
System.out.println(F + " with Big Decimal (2 places) is " + withBigDecimal(F, 2));
|
||||
System.out.println(F + " with Big Decimal (3 places) is " + withBigDecimal(F, 3));
|
||||
System.out.println(D + " with Math.round is (2 places) " + withMathRound(D, 2));
|
||||
System.out.println(D + " with Math.round is (3 places) " + withMathRound(D, 3));
|
||||
System.out.println(F + " with Math.round is (2 places) " + withMathRound(F, 2));
|
||||
System.out.println(F + " with Math.round is (3 places) " + withMathRound(F, 3));
|
||||
System.out.println(D + " with String Format is (2 places) " + withStringFormat(D, 2));
|
||||
System.out.println(D + " with String Format is (3 places) " + withStringFormat(D, 3));
|
||||
System.out.println(F + " with String Format is (2 places) " + withStringFormat(F, 2));
|
||||
System.out.println(F + " with String Format is (3 places) " + withStringFormat(F, 3));
|
||||
System.out.println(D + " with Decimal Format (local) is " + withDecimalFormat(D, 0));
|
||||
System.out.println(D + " with Decimal Format (2 places) is " + withDecimalFormat(D, 2));
|
||||
System.out.println(D + " with Decimal Format (3 places) is " + withDecimalFormat(D, 3));
|
||||
System.out.println(F + " with Decimal Format is (local) " + withDecimalFormat(F, 0));
|
||||
System.out.println(F + " with Decimal Format is (2 places) " + withDecimalFormat(F, 2));
|
||||
System.out.println(F + " with Decimal Format is (3 places) " + withDecimalFormat(F, 3));
|
||||
}
|
||||
|
||||
public static double withBigDecimal(double value, int places) {
|
||||
if (places < 0)
|
||||
throw new IllegalArgumentException();
|
||||
|
||||
BigDecimal bigDecimal = new BigDecimal(value);
|
||||
bigDecimal = bigDecimal.setScale(places, RoundingMode.HALF_UP);
|
||||
return bigDecimal.doubleValue();
|
||||
}
|
||||
|
||||
public static double withMathRound(double value, int places) {
|
||||
double scale = Math.pow(10, places);
|
||||
return Math.round(value * scale) / scale;
|
||||
}
|
||||
|
||||
public static double withDecimalFormat(double value, int places) {
|
||||
DecimalFormat df = (DecimalFormat) NumberFormat.getNumberInstance(Locale.getDefault());
|
||||
DecimalFormat df2 = new DecimalFormat("#,###,###,##0.00");
|
||||
DecimalFormat df3 = new DecimalFormat("#,###,###,##0.000");
|
||||
if (places == 2)
|
||||
return new Double(df2.format(value));
|
||||
else if (places == 3)
|
||||
return new Double(df3.format(value));
|
||||
else
|
||||
return new Double(df.format(value));
|
||||
}
|
||||
|
||||
public static String withStringFormat(double value, int places) {
|
||||
return String.format("%." + places + "f", value);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
package com.baeldung.formatNumber;
|
||||
|
||||
import org.apache.commons.math3.util.Precision;
|
||||
import org.decimal4j.util.DoubleRounder;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class FormatNumberTest {
|
||||
private double value = 2.03456d;
|
||||
private int places = 2;
|
||||
private double expected = 2.03d;
|
||||
|
||||
@Test public void givenDecimalNumber_whenFormatNumberToNDecimalPlaces_thenGetExpectedResult() {
|
||||
double delta = 0.0d;
|
||||
Assert.assertEquals(expected, FormatNumber.withBigDecimal(value, places), delta);
|
||||
Assert.assertEquals(expected, FormatNumber.withDecimalFormat(value, places), delta);
|
||||
Assert.assertEquals(expected, FormatNumber.withMathRound(value, places), delta);
|
||||
|
||||
places = 3;
|
||||
expected = 2.035d;
|
||||
|
||||
Assert.assertEquals(expected, FormatNumber.withBigDecimal(value, places), delta);
|
||||
Assert.assertEquals(expected, FormatNumber.withDecimalFormat(value, places), delta);
|
||||
Assert.assertEquals(expected, FormatNumber.withMathRound(value, places), delta);
|
||||
|
||||
value = 256.024d;
|
||||
places = 2;
|
||||
expected = 256.02d;
|
||||
|
||||
Assert.assertEquals(expected, FormatNumber.withBigDecimal(value, places), delta);
|
||||
Assert.assertEquals(expected, FormatNumber.withDecimalFormat(value, places), delta);
|
||||
Assert.assertEquals(expected, FormatNumber.withMathRound(value, places), delta);
|
||||
|
||||
value = 260.773d;
|
||||
places = 2;
|
||||
expected = 260.77d;
|
||||
|
||||
Assert.assertEquals(expected, FormatNumber.withBigDecimal(value, places), delta);
|
||||
Assert.assertEquals(expected, FormatNumber.withDecimalFormat(value, places), delta);
|
||||
Assert.assertEquals(expected, FormatNumber.withMathRound(value, places), delta);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue