diff --git a/src/routines/NumberUtil.java b/src/routines/NumberUtil.java index 547993e..0dc32d4 100644 --- a/src/routines/NumberUtil.java +++ b/src/routines/NumberUtil.java @@ -436,7 +436,7 @@ public static String numberToPercent2Scale(Double number) { */ public static String numberToString(Number number) { if (number != null) { - NumberFormat nf = NumberFormat.getInstance(Locale.GERMANY); + NumberFormat nf = NumberFormat.getInstance(Locale.ENGLISH); return nf.format(number); } else { return ""; @@ -451,6 +451,32 @@ public static String numberToString(Number number) { * {talendTypes} String * * {param} double(2.5) number: number to format + * {param} boolean(false) german: number to format + * {param} boolean(false) grouping: number to format + * {param} int(2) maxfraction: number to format + * + * {example} numberToString(1234.12345,false,false,2) result: "1,234.12345" + * + */ + public static String numberToString(Number number, boolean german, boolean grouping, int maxFractionDigits) { + if (number != null) { + NumberFormat nf = NumberFormat.getInstance(german ? Locale.GERMAN : Locale.ENGLISH); + nf.setGroupingUsed(grouping); + nf.setMaximumFractionDigits(maxFractionDigits); + return nf.format(number); + } else { + return ""; + } + } + + /** + * formats the number + * + * {Category} NumberUtil + * + * {talendTypes} String + * + * {param} double(2.5) number: number to format * * {example} numberToStringRoundScale2(1234.12345) result: "1.234,12" * diff --git a/src_test/routines/test/TestNumberUtil.java b/src_test/routines/test/TestNumberUtil.java index bb88381..950a935 100644 --- a/src_test/routines/test/TestNumberUtil.java +++ b/src_test/routines/test/TestNumberUtil.java @@ -19,7 +19,7 @@ public void testSortLongNumberList() throws Exception { @Test public void testRoundScaleN() { - double expected = 6.675d; + double expected = 6.68d; double test = 6.675000000000003d; int precision = 2; double actual = NumberUtil.roundScaleN(test, precision); @@ -37,4 +37,13 @@ public void testEqualsWithPrecision() { assertTrue("Positive test failed", NumberUtil.equals(t1, t2, precision)); } + @Test + public void testNumber2String() { + double d = 100000000d; + String expected = "100000000"; + String actual = NumberUtil.numberToString(d, false, false, 4); + System.out.println(actual); + assertEquals("Format wrong", expected, actual); + } + }