Java 如何格式化字符串数字以在 android 编辑字段中包含逗号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19788113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I format a String number to have commas in android Edit Field
提问by user1082964
For what function I can use in android to display the number into different formats.
我可以在android中使用什么功能将数字显示为不同的格式。
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.
例如:如果我输入 1000,那么它应该像这样显示 1,000。如果我输入 10000,那么它应该像这样显示 10,000。如果我输入 1000000,那么它应该像这样显示 1,000,000。
Please guide me.
请指导我。
采纳答案by Neil
You could use DecimalFormat
and just format the number
您可以使用DecimalFormat
并格式化数字
DecimalFormat formatter = new DecimalFormat("#,###,###");
String yourFormattedString = formatter.format(100000);
The result will be
结果将是
1,000,000
for 100000010,000
for 100001,000
for 1000
1,000,000
100000010,000
100001,000
1000
Update 12/02/2019
更新 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
这String.format("%,d", number)
将是一个更好(更少硬编码)的解决方案,如@DreaminginCode 在下面的评论中所示,所以我想我会在这里添加它作为替代
回答by Sandeep
try this one hope it will help.
试试这个希望它会有所帮助。
System.out.println(NumberFormat.getNumberInstance(Locale.US).format(1000));
回答by Eldhose M Babu
Add a text change listener as below (Also make sure that the input type selected for Edittext is Number) :
添加一个文本更改侦听器,如下所示(还要确保为 Edittext 选择的输入类型是数字):
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
}
});
回答by YLS
private String getFormatedAmount(int amount){
return NumberFormat.getNumberInstance(Locale.US).format(amount);
}
回答by Codeversed
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
回答by Nail Shaykhraziev
You can use Numberformat
您可以使用数字格式
public static double getNumberByString(String s) throws ParseException {
return NumberFormat.getInstance(Locale.getDefault()).parse(s).doubleValue();
}
回答by Kush
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");
回答by Atiar Talukdar
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));
}