How can I format a String number to have commas in android Edit Field
85,464
Solution 1
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 1000
Update 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
Solution 2
try this one hope it will help.
System.out.println(NumberFormat.getNumberInstance(Locale.US).format(1000));
Solution 3
private String getFormatedAmount(int amount){
return NumberFormat.getNumberInstance(Locale.US).format(amount);
}
Solution 4
int[] numbersToFormat = new int[]
{ 1, 10, 100, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 };
for (int number : numbersToFormat) {
System.out.println(
NumberFormat.getNumberInstance(Locale.getDefault()).format(number));
}
OUTPUT
1
10
100
10,000
100,000
1,000,000
10,000,000
100,000,000
1,000,000,000
Solution 5
Add this function in common class
public static String getFormatedNumber(String number){
if(!number.isEmpty()) {
double val = Double.parseDouble(number);
return NumberFormat.getNumberInstance(Locale.US).format(val);
}else{
return "0";
}
}
And use that function every where like this:
String newNumber = Utils.getFormatedNumber("10000000");
Related videos on Youtube
Author by
user1082964
Updated on November 22, 2021Comments
-
user1082964 over 2 years
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.
-
user1082964 over 10 yearsbut if i enter 100000 it displaying 100,000 instead i need like this 1,00,000, is it possible?
-
the-ginger-geek over 10 yearsWhy would you want to do that? 100,000 is standard. Try moving the comma in the hash format like this #,##,###. Don't know if it will work but you can give it a try.
-
Matt about 10 yearsTo be safe, you'll likely want to use the device's locale, not always US. Locale current = getResources().getConfiguration().locale;
-
Gober over 9 yearsLocale.getDefault() is an even easier way to get device locale.
-
Juan De la Cruz about 6 yearsAccording to Java Documentation, parameterless
get...Instance()
methods use default locale by default. -
gonephishing almost 6 years@Neil, the standard is only valid for specific countries. Like for example, India has a different standard and is of the format #,##,###
-
Hai Zhang over 5 yearsNot to mention countries using dot
.
as thousands separator. This is such a bad but popular hard-coding answer. Developers should always respect user's locale unless there's a very good reason for not doing so. -
the-ginger-geek over 5 years@dreamingincode this answer, answers the question. If the question gave more insight into the actual problem the answer might have been different. It is not my place to judge good or bad practice with insufficient information. Practice can only be judged in practice. I agree that using locale would be a better solution if the application was to be used world wide.
-
Hai Zhang over 5 yearsIt answers the question but answering a question doesn't automatically make it a good answer. And when information is insufficient, I believe one as a programmer should assume less instead of more. Answers that use hard-coding can break easily when assumptions change, and in this case hard-coding is especially bad for any i18n related code because there can always be future people trying to add i18n and it would be their wtf moment. Not to mention even just a larger number can break this. For a much simpler solution, you could have used
String.format("%,d", number)
which is short and correct -
Leon Zaii over 5 yearscan I add decimal behind like 1,000,000.50 ? Or 1M.50 ? @Neil