Reputation: 915
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.
Upvotes: 91
Views: 96347
Reputation: 11032
Recommended and preferred way is to do it with the strings.xml
file
<string name="format_denominated">%,d</string>
from your Kotlin/Java code
getResources().getString(R.string.format_denominated, value)
Even better with databinding
<TextView
android:text="@{@string/format_denominated(value)}"
............/>
Upvotes: 1
Reputation: 111
I wrote this Kotlin extension function may can help.
fun String.formatPoint(): String {
val sb = StringBuilder()
this.reversed().forEachIndexed { index, c ->
// 123,123,123
if ((index + 1) % 3 == 0) {
if (index != this.length - 1) {
sb.append("$c,")
} else {
sb.append(c)
}
} else {
sb.append(c)
}
}
return sb.toString().reversed()
}
Upvotes: 0
Reputation: 746
public static String formatNumber(int number){
return NumberFormat.getNumberInstance(Locale.getDefault()).format(number);
}
public static String formatNumber(String number){
return NumberFormat.getNumberInstance(Locale.getDefault()).format(Integer.parseInt(number));
}
Upvotes: 1
Reputation: 7081
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
Upvotes: 182
Reputation: 1092
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");
Upvotes: 4
Reputation: 452
You can use Numberformat
public static double getNumberByString(String s) throws ParseException {
return NumberFormat.getInstance(Locale.getDefault()).parse(s).doubleValue();
}
Upvotes: 3
Reputation: 9473
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
Upvotes: 9
Reputation: 1565
private String getFormatedAmount(int amount){
return NumberFormat.getNumberInstance(Locale.US).format(amount);
}
Upvotes: 16
Reputation: 14510
Add a text change listener as below (Also make sure that the input type selected for Edittext is Number) :
etTest.addTextChangedListener(new TextWatcher() {
boolean isManualChange = false;
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if (isManualChange) {
isManualChange = false;
return;
}
try {
String value = s.toString().replace(",", "");
String reverseValue = new StringBuilder(value).reverse()
.toString();
StringBuilder finalValue = new StringBuilder();
for (int i = 1; i <= reverseValue.length(); i++) {
char val = reverseValue.charAt(i - 1);
finalValue.append(val);
if (i % 3 == 0 && i != reverseValue.length() && i > 0) {
finalValue.append(",");
}
}
isManualChange = true;
etTest.setText(finalValue.reverse());
etTest.setSelection(finalValue.length());
} catch (Exception e) {
// Do nothing since not a number
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
Upvotes: 2
Reputation: 2593
try this one hope it will help.
System.out.println(NumberFormat.getNumberInstance(Locale.US).format(1000));
Upvotes: 52