Split format number with decimal format method into two separate methods

This commit is contained in:
Marcin Krykowski 2020-05-25 09:04:14 +01:00
parent dcb9afaaef
commit e14c2c188d
1 changed files with 13 additions and 11 deletions

View File

@ -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) {