-5

How do I format a string with commas to display in curreny format?

Assume the incoming string is a big number. For example:

"1234567901234567890123456"

I want result as:

"12,345,678,901,234,567,890,123,456"

The DecimalFormat in Java has a limitation to format a big number.

chamina
  • 498
  • 3
  • 15

5 Answers5

7

95% of problems are solved by clarifying your requirements. Let's filter out the things that are definitely not requirements. "incoming string is a big number" - that statement can never hold true. Is it a String? If yes, then it's a String, and never a number. This distinction is important. I'll assume that the answer to this question is "yes".

Now that you have a String, why are you using a DecimalFormat? What does a String have to do with a decimal number formatter? Nothing related to the problem that you are trying to solve. I'll leave the determination of the correct approach to solve your problem up to you - after all, my speculations at your requirements may not be correct.

To reiterate, here are the things that are definitely incorrect: - You have a String, that is a big number" - DecimalFormat has an issue with "big numbers" - an unsubstantiated conclusion drawn on a false basis of understanding.

2
System.out.println(NumberFormat.getNumberInstance(Locale.US).format(35634646));

Output: 35,634,646

haMzox
  • 2,073
  • 1
  • 12
  • 25
  • 1
    this not work with the OP number `System.out.println(NumberFormat.getNumberInstance(Locale.US).format("1234567901234567890123456"));` – Youcef LAIDANI May 26 '17 at 14:01
  • 1
    @hamzox Answer can be "accepted" only when question is at least 15 minutes old. Also in current form you are not handling big numbers as OP shows in question. You can use BigInteger here. – Pshemo May 26 '17 at 14:03
  • Wait, I am modifying my answer. – haMzox May 26 '17 at 14:03
  • In other words you can replace `.format(35634646)` with `.format(new BigInteger("12345678901234567890123456")`. – Pshemo May 26 '17 at 14:18
2

This would do

double formatThis = Double.parseDouble(yourNumber);
DecimalFormat formatter = new DecimalFormat("#,###.00");
System.out.println(formatter.format(formatThis));
parlad
  • 1,143
  • 4
  • 23
  • 42
  • 1
    this print `1 234 567 901 234 567 800 000 000,00` ?? – Youcef LAIDANI May 26 '17 at 14:00
  • @YCF_L, this will print `1,234,567,901,234,567,800,000,000.00 ` , any doubt? – parlad May 26 '17 at 14:10
  • no, idea, i don't know in my system it print this `1 234 567 901 234 567 800 000 000,00` – Youcef LAIDANI May 26 '17 at 14:11
  • i also have no idea, why your system prints that way , could be good question , post it , happy to see. – parlad May 26 '17 at 14:12
  • you need maybe to define the Local for that i get this instead your input take a look here https://stackoverflow.com/questions/5054132/how-to-change-the-decimal-separator-of-decimalformat-from-comma-to-dot-point – Youcef LAIDANI May 26 '17 at 14:14
  • It is locale dependent. Use locale which have commas instead of spaces, like `Locale.US`. But `Double.parseDouble(yourNumber);` doesn't solve OP problem since `Double.parseDouble("1234567901234567890123456");` requires more precision than double can give and it parses it to `1 234 567 901 234 567 800 000 000,00` which is *not* what OP asked. – Pshemo May 26 '17 at 14:16
1

You should be able to achieve the desired using the expression posted below:

(?n:(^\$?(?!0,?\d)\d{1,3}(?=(?<1>,)|(?<1>))(\k<1>\d{3})*(\.\d\d)?)$)
davidxxx
  • 125,838
  • 23
  • 214
  • 215
ryathind
  • 29
  • 7
1

Try this.

String input = "12345678901234567890123456";
String output = String.format("%,d", new BigInteger(input));
System.out.println(output);
// -> 12,345,678,901,234,567,890,123,456
  • 1
    Use `String.format(Locale.US, "%,d", new BigInteger(input));` to have commas instead of spaces. – Pshemo May 26 '17 at 14:11
  • @Pshemo, this throws an exception : `java.lang.NumberFormatException: For input string: "012‌​3456"` – davidxxx May 26 '17 at 14:25
  • 1
    @davidxxx You have some unprintable characters in your string between 2 and 3. Take a look at result of `Arrays.toString("012‌​3456".chars().toArray())`. It is [48, 49, 50, 8204, 8203, 51, 52, 53, 54] so you have there `8204` which is [Zero Width Non-Joiner](https://unicode-table.com/en/200C/) and `8203` which is [Zero Width Space](https://unicode-table.com/en/200B/). – Pshemo May 26 '17 at 14:39
  • @Pshemo You are totally right. Thank a lot for this explanation. I remark also it when I double click on the text, it select only the first part as if the String was split into 2. I had never encountered this behavior on my IDE. – davidxxx May 26 '17 at 14:48