Skip to content

Commit

Permalink
number2String with grouping and max fraction digit added
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Lolling committed May 19, 2021
1 parent 6add1e7 commit 71c9dfe
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
28 changes: 27 additions & 1 deletion src/routines/NumberUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 "";
Expand All @@ -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"
*
Expand Down
11 changes: 10 additions & 1 deletion src_test/routines/test/TestNumberUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}

}

0 comments on commit 71c9dfe

Please sign in to comment.