4

I just want the numbers to be output in "well readable" format. e.g. you can output 100000 as 100.000 which would be more readable.

Is there an already existing method that can be used for this?

Aniruddh Parihar
  • 3,072
  • 3
  • 21
  • 39
forty4seven
  • 73
  • 1
  • 4

2 Answers2

7

You can use NumberFormat:

int n = 100000;
String formatted = NumberFormat.getInstance().format(n);
System.out.println(formatted);

Output:

100,000
Majed Badawi
  • 27,616
  • 4
  • 25
  • 48
  • 5
    It shouldl be mentioned that [`NumberFormat.getInstance()`](https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/text/NumberFormat.html#getInstance()) returns an instance that is configured with the system locale. So on a german system, for example, the output should be `100.000`. The locale can be explicitly configured by calling [`NumberFormat.getinstance(Locale)`](https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/text/NumberFormat.html#getInstance(java.util.Locale)) – Turing85 Aug 30 '20 at 11:29
  • 1
    thx i got it NumberFormat.getInstance().format(variable); – forty4seven Aug 30 '20 at 11:33
0

You can use DecimalFormat (Java 7+):

var dfs = new DecimalFormatSymbols();
dfs.setGroupingSeparator('.');
var df = new DecimalFormat("###,##0", dfs);
System.out.println(df.format(100000));

output:

100.000

DecimalFormat has more flexibility to convert numbers to string by pattern.

kozmo
  • 4,024
  • 3
  • 30
  • 48
  • [Double-brace initialization is bad.](https://stackoverflow.com/questions/1958636/what-is-double-brace-initialization-in-java) Don’t use it. Also, NumberFormat.getInstance is better than explicit use of DecimalFormat, especially when trying to conform to the default locale or a particular locale. – VGR Aug 30 '20 at 16:15
  • fix `Double-brace initialization` – kozmo Aug 30 '20 at 16:51
  • @VGR - why is `DecimalFormat` worse than `NumberFormat`? I think DecimalFormat is more flexible... – kozmo Aug 30 '20 at 18:43
  • In most cases, applications will want to display a number in a format corresponding to the current locale. `NumberFormat.getInstance()` will do that. For example, not every country uses uses `.` as the grouping separator. From the [DecimalFormat documentation](https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/text/DecimalFormat.html), “The grouping separator is commonly used for thousands, but in some countries it separates ten-thousands.” You *could* account for that yourself, but using NumberFormat.getInstance gives you multicultural support for no additional effort. – VGR Aug 30 '20 at 18:55
  • @VGR - but if you want to customized format (for example, group or decimal separator) you have to use `DecimalFormat`. – kozmo Aug 30 '20 at 19:13
  • 1
    Yes, if you want to override the locale settings, then you would use DecimalFormat directly. The question is too short to determine whether that’s the goal, but since it doesn’t mention a custom format, I assume the common case is the desired case. – VGR Aug 30 '20 at 19:19