For what function I can use in android to display the number into different formats.
For eg: If I enter 1000 then it should display like this 1,000. If I enter 10000 then it should display like this 10,000. If I enter 1000000 then it should display like this 1,000,000.
Please guide me.
For format String "%,. 2f" means separate digit groups with commas and ".
In this example, the format string has two arguments: %1$s is a string and %2$d is a decimal integer. You can format the string with arguments from your application like this: Resources res = getResources(); String text = res.getString(R. string.welcome_messages, username, mailCount);
You can use java. util. text. NumberFormat class and its method setGroupingUsed(true) and setGroupingSize(3) to group numbers and add a comma between them.
You could use DecimalFormat
and just format the number
DecimalFormat formatter = new DecimalFormat("#,###,###"); String yourFormattedString = formatter.format(100000);
The result will be
1,000,000
for 100000010,000
for 100001,000
for 1000Update 12/02/2019
This String.format("%,d", number)
would be a better(less hardcoded) solution as indicated in the comments below by @DreaminginCode so I thought I would add it here as an alternative
try this one hope it will help.
System.out.println(NumberFormat.getNumberInstance(Locale.US).format(1000));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With