From 6c753fb2042cd8db1f18ba3aaef7caef267d2bc7 Mon Sep 17 00:00:00 2001 From: Marcin Krykowski Date: Fri, 22 May 2020 22:30:57 +0100 Subject: [PATCH] Add Format Number implementation and tests --- .../baeldung/formatNumber/FormatNumber.java | 63 +++++++++++++++++++ .../formatNumber/FormatNumberTest.java | 42 +++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 java-numbers/src/main/java/com/baeldung/formatNumber/FormatNumber.java create mode 100644 java-numbers/src/test/java/com/baeldung/formatNumber/FormatNumberTest.java diff --git a/java-numbers/src/main/java/com/baeldung/formatNumber/FormatNumber.java b/java-numbers/src/main/java/com/baeldung/formatNumber/FormatNumber.java new file mode 100644 index 0000000000..e76fa2cac9 --- /dev/null +++ b/java-numbers/src/main/java/com/baeldung/formatNumber/FormatNumber.java @@ -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); + } +} diff --git a/java-numbers/src/test/java/com/baeldung/formatNumber/FormatNumberTest.java b/java-numbers/src/test/java/com/baeldung/formatNumber/FormatNumberTest.java new file mode 100644 index 0000000000..0cc035f246 --- /dev/null +++ b/java-numbers/src/test/java/com/baeldung/formatNumber/FormatNumberTest.java @@ -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); + } +}