From e14c2c188dc90d41b3f193030434e21491144161 Mon Sep 17 00:00:00 2001 From: Marcin Krykowski Date: Mon, 25 May 2020 09:04:14 +0100 Subject: [PATCH] Split format number with decimal format method into two separate methods --- .../baeldung/formatNumber/FormatNumber.java | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/java-numbers/src/main/java/com/baeldung/formatNumber/FormatNumber.java b/java-numbers/src/main/java/com/baeldung/formatNumber/FormatNumber.java index e76fa2cac9..4ce93adae0 100644 --- a/java-numbers/src/main/java/com/baeldung/formatNumber/FormatNumber.java +++ b/java-numbers/src/main/java/com/baeldung/formatNumber/FormatNumber.java @@ -23,12 +23,12 @@ public class FormatNumber { 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)); + System.out.println(D + " with Decimal Format (local) is " + withDecimalFormatLocal(D)); + System.out.println(D + " with Decimal Format (2 places) is " + withDecimalFormatPattern(D, 2)); + System.out.println(D + " with Decimal Format (3 places) is " + withDecimalFormatPattern(D, 3)); + System.out.println(F + " with Decimal Format is (local) " + withDecimalFormatLocal(F)); + System.out.println(F + " with Decimal Format is (2 places) " + withDecimalFormatPattern(F, 2)); + System.out.println(F + " with Decimal Format is (3 places) " + withDecimalFormatPattern(F, 3)); } public static double withBigDecimal(double value, int places) { @@ -45,16 +45,18 @@ public class FormatNumber { return Math.round(value * scale) / scale; } - public static double withDecimalFormat(double value, int places) { - DecimalFormat df = (DecimalFormat) NumberFormat.getNumberInstance(Locale.getDefault()); + public static double withDecimalFormatPattern(double value, int places) { 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) + if (places == 3) return new Double(df3.format(value)); - else - return new Double(df.format(value)); + } + + public static double withDecimalFormatLocal(double value) { + DecimalFormat df = (DecimalFormat) NumberFormat.getNumberInstance(Locale.getDefault()); + return new Double(df.format(value)); } public static String withStringFormat(double value, int places) {