How to display a phone number using decimal format

Hello, I would like to use DecimalFormat to display:

8392472 how

839 24 72

I tried

 DecimalFormat dc = new DecimalFormat("000 00 00"); return dc.format(number); 

I also tried "### ## ##"

+4
source share
3 answers

I do not think you can do this with DecimalFormat , because your spaces are not group or decimal separators.

A simple way would be to simply use a string:

 int number = 8392472; String s = String.valueOf(number); String formatted = s.substring(0, 3) + " " + s.substring(3, 5) + " " + s.substring(5, 7); 
+1
source

I think the easiest way to achieve this is as shown below.

 public static void main(String[] args) { int number = 8392472; StringBuilder sb = new StringBuilder(String.valueOf(number)) .insert(3," ") .insert(6," "); System.out.println(sb.toString()); } 
0
source

Use the Google Library! This works well for us.

https://code.google.com/p/libphonenumber/

-1
source

All Articles