我有一个 TextView
,它包含一个数字值(不断更新)。有没有一种方法可以在数字增加时自动添加逗号?
这是我当前的代码:
String number = textView.getText().toString();
double amount = Double.parseDouble(number);
DecimalFormat formatter = new DecimalFormat("#,###");
String formatted = formatter.format(amount);
textView.setText(formatted);
最佳答案
接受一个整数并返回一个格式化为美国 Locale
private String getFormatedAmount(int amount){
return NumberFormat.getNumberInstance(Locale.US).format(amount);
}
输入:10
输出:10
输入:100
输出:100
输入:1000
输出:1,000
输入:10000
输出:10,000
输入:100000
输出:100,000
输入:1000000
输出:1,000,000
关于android - 如何在Android中自动为大数字添加逗号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18478411/