4

Formatting number 234156.123 with pattern #,##,###.### using the below code is getting formatted as 234,156.123 but instead it should get formatted as 2,34,156.123

Decimal places is as per pattern whereas the numbers before decimal point is not formatting as expected.

Code written is as follows

String pattern = "#,##,###.###";
double number = 234156.123;
DecimalFormat decimalFormat = new DecimalFormat(pattern);
String format = decimalFormat.format(number);
System.out.println(format);

Result:

234,156.123


Expected Result:

2,34,156.123

altis
  • 41
  • 3
  • 2
    It's all in the docs: https://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html –  Jun 22 '15 at 09:09
  • 1
    IMHO this is not a duplicate. OP's problem is not the decimal separator. – Marvin Jun 22 '15 at 09:14
  • @Ahmad Can you explain why you think that this question is a duplicate of the one you've linked? Where do you see the question about the conversion of the decimal separator types? – Tom Jun 22 '15 at 09:14
  • @Gosu: can you explain why you think that *234,156.123* is "source code? Since you've formatted it as that, you have a reason, right? – Tom Jun 22 '15 at 09:17
  • @Tom I tend to do that for output, since I find it easier to read it when looking at the question. It's not source code, just trying to make it easier for others to see what's the issue stated in the question. – almightyGOSU Jun 22 '15 at 09:20
  • @Gosu [Then please stop doing that](http://meta.stackoverflow.com/questions/254990/when-should-code-formatting-be-used-for-non-code-text). Either use blockquote, or write it *italic* or whatever. And there is also no need to write the expected result in **bold**. – Tom Jun 22 '15 at 09:23
  • 1
    @Tom okay, sure. Will keep that in mind. – almightyGOSU Jun 22 '15 at 09:24
  • @Gosu: Thank you. Try to use blockquote for output. This is almost always the better way. If you need a break or an empty line, then add `
    `. And _only_ if you need a formatted output (like in a "self made table"), then use "code formatting", because this is currently the only way to format indentations correctly. (Edit: about your new edit: looks much better :)).
    – Tom Jun 22 '15 at 09:28

2 Answers2

0

Following on from Alex's comment docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html

The grouping separator is commonly used for thousands, but in some countries it separates ten-thousands. The grouping size is a constant number of digits between the grouping characters, such as 3 for 100,000,000 or 4 for 1,0000,0000. If you supply a pattern with multiple grouping characters, the interval between the last one and the end of the integer is the one that is used. So "#,##,###,####" == "######,####" == "##,####,####".

To format in this way you will need to write a custom formatter.

Jaydee
  • 4,138
  • 1
  • 19
  • 20
0

Unfortunately, DecimalFormat doesn't do what you want.

From the Javadoc:

The grouping size is a constant number of digits between the grouping characters, such as 3 for 100,000,000 or 4 for 1,0000,0000. If you supply a pattern with multiple grouping characters, the interval between the last one and the end of the integer is the one that is used. So "#,##,###,####" == "######,####" == "##,####,####".

Your desired behaviour has a non-constant group size - 3 preceded by 2 preceded by 1. You will need to write your own formatter.

slim
  • 40,215
  • 13
  • 94
  • 127