How to add empty space inside int?
Asked Answered
J

7

7

Let's say I want to print the number 100000000. At first sight it is difficult to tell how many millions this number is representing. Is it 10 million or 100 million? How can I make big numbers in Java look more readable? Something like this for instance would be great: 100 000 000. You can tell right away that the number is 100 million.

Johnsiejohnson answered 21/4, 2015 at 17:5 Comment(5)
You can use underscore for big number.Waterfront
@Masud OP wants to add white spaces when printing the number, not in the source codeOfficiary
docs.oracle.com/javase/tutorial/java/data/numberformat.htmlEmancipation
@Turing85, He asked a question like how can I make big numbers in Java look more readable?Waterfront
First line of my post: Let's say I want to print the number 100000000. Johnsiejohnson
O
10

You can also try DecimalFormat;

DecimalFormat formatter = new DecimalFormat("#,###");
System.out.println(formatter.format(100000));

Results:

1000>>1,000
10000>>10,000
100000>>100,000
1000000>>1,000,000
Oast answered 21/4, 2015 at 17:13 Comment(0)
D
4

You can try like this:

String.format("%.2fM", yourNumber/ 1000000.0);

This will display the numbers in the format

1,000,000 => 1.00M
1,234,567 => 1.23M

EDIT:-

I know its a late edit but yes there is one more way:

private static String[] suff = new String[]{"","k", "m", "b", "t"};
private static int MAX_LENGTH = 4;

private static String numberFormat(double d) {
    String str = new DecimalFormat("##0E0").format(d);
    str = str.replaceAll("E[0-9]", suff[Character.getNumericValue(str.charAt(str.length() - 1)) / 3]);
    while(str.length() > MAX_LENGTH || str.matches("[0-9]+\\.[a-z]")){
        str = str.substring(0, str.length()-2) + str.substring(str.length() - 1);
    }
    return str;
}

Call this function and you will get the output as follows:

201700 = 202k
3000000 = 3m
8800000 = 8.8m
Divebomb answered 21/4, 2015 at 17:10 Comment(0)
S
3

Use the DecimalFormat class, see the link for how to use.

To save you searching I have written what you basically need

DecimalFormat myFormatter = new DecimalFormat("### ### ###");
String output = myFormatter.format(value);
System.out.println(output);
Stingaree answered 21/4, 2015 at 17:18 Comment(0)
E
2

You can use decimal format to format the string

    DecimalFormat decimalFormat = new DecimalFormat("###,###,###");
    System.out.println(decimalFormat.format(100000000));

This will print 100,000,000

For other input - say 1000 it would print 1,000

Expatiate answered 21/4, 2015 at 17:17 Comment(0)
L
2

Using a DecimalFormat, as already suggested, is almost everything you need. To get the exact result that you asked for, say space-separated thousand's and so on, you need to combine it with DecimalFormatSymbols, like:

DecimalFormatSymbols customSymbols = DecimalFormatSymbols.getInstance(Locale.US); 
customSymbols.setGroupingSeparator(' '); 
new DecimalFormat("#,###;-#,###", customSymbols).format(value);

; semicolon in "#,###;-#,###" separates positive from negative numbers (which must be defined separately, or you will end up with all converted to be positive!)

, colon points a place to separate number (each 3 digits in this example).

For instance, for

value=12345678

the output would be

12 345 678
Lepanto answered 9/8, 2019 at 12:36 Comment(0)
P
0

You will probably just want to use a string for this. If you need to do some calculations simply keep it as an int until it needs to be printed. Then when it needs to be printed convert it to a string, process the string so that its as readable as you would like, and print it.

Pericardium answered 21/4, 2015 at 17:7 Comment(0)
E
0

how about below approach, but it's supported in Java 7 and later versions:

int twoMillion = 2_000_000;

Eadie answered 21/4, 2015 at 17:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.