0

I want to format always-11-digit integer number to format "123-456-789 12". Is there library method to do this?

EDIT: May be I can group 3 numbers with:

 DecimalFormatSymbols decimalFormatSymbols = new DecimalFormatSymbols();
 decimalFormatSymbols.setGroupingSeparator('-');
 DecimalFormat decimalFormat = new DecimalFormat("000,000,000.00", decimalFormatSymbols);

But I can't separate two first digit with space (where '.' is now)

Dmitry Torshin
  • 113
  • 1
  • 6
  • Probably not since it would require 10 lines of code to do it. – Arnaud Denoyelle Jun 26 '17 at 08:19
  • u mean Java isn't enough? – Michael Shrestha Jun 26 '17 at 08:20
  • See the String#format answer for example –  Jun 26 '17 at 08:21
  • Just take care that your input might not be a number but actually be a string composed of digits. For example "000 456 789 12". – Arnaud Denoyelle Jun 26 '17 at 08:21
  • Sorry for bad explanation. java.lang.NumberFormat can set any one group separator, like '-' too. But I need 2 different group separator at time '-' and ' '. – Dmitry Torshin Jun 26 '17 at 08:24
  • @RC I don't understand how `String.format` can do it (except if you manually isolate each group of digits). – Arnaud Denoyelle Jun 26 '17 at 08:27
  • @DmitryTorshin I ended up with this solution. 1) convert your number to a string and optionally pad it with zeroes : `String numberAsStr = String.format("%011d", number);`. Then 2), use some `String.substring` : `return String.format("%s-%s-%s %s", numberAsStr.substring(0, 3), numberAsStr.substring(3, 6), numberAsStr.substring(6, 9), numberAsStr.substring(9, 11));` – Arnaud Denoyelle Jun 26 '17 at 08:34
  • @ArnaudDenoyelle Yes, I don't want to separate in manualy. Hope there are better solution. – Dmitry Torshin Jun 26 '17 at 08:36
  • @ArnaudDenoyelle thank you. I understand this solution (I prefer to use myNumber / 1000_000_000 , myNumber / 1000_000 % 1000 etc.). So there are no better and easier solution? – Dmitry Torshin Jun 26 '17 at 08:39
  • @ArnaudDenoyelle the way you did in your comment !? –  Jun 26 '17 at 08:46
  • Use your decimalformat and then `.replace(".", " ")` (beware of the Locale) or something like that –  Jun 26 '17 at 08:48
  • @RC. Do you think it's a good way? Ok, thank you. – Dmitry Torshin Jun 26 '17 at 08:49
  • I would use the substring. –  Jun 26 '17 at 08:49
  • 1
    @RC. Is any question about formatting numbers in Java is a duplicate of general formatting question? – DAle Jun 26 '17 at 08:50
  • 1
    @DAle do we need one question per format?? –  Jun 26 '17 at 08:53
  • @RC., No, but that general question doesn't have an answer to this question. – DAle Jun 26 '17 at 08:56
  • `DecimalFormatSymbols s = new DecimalFormatSymbols(); s.setGroupingSeparator('-'); DecimalFormat f = new DecimalFormat("000,000,000"); f.setDecimalFormatSymbols(s); return String.format("%s %02d", f.format(number / 100), number % 100);` – DAle Jun 26 '17 at 09:20
  • 1
    @DAle the duplicate already mention a lot of possibilities. Adding some research from there should be enough to answer almost every number format problem. So I don't believe this is necessary. Of course, this case is specific and need some "workaround" for every solution but that's the job of the developer to match a solution to his problem, without that, we would not have a job – AxelH Jun 26 '17 at 09:51

0 Answers0