How to get Android to automatically add commas in large numbers?

So, just a pretty straightforward question. I have a text view that contains a numeric value (which is constantly updated. So is there a method that I can use to automatically add a comma if the number increases? I know there is a method in Visual Basic, but I'm still pretty new to the platform android / java.

Edit:

String number = textView1.getText().toString(); double amount = Double.parseDouble(number); DecimalFormat formatter = new DecimalFormat("#,###"); String formatted = formatter.format(amount); textView1.setText(formatted); 

This does not work?

+4
source share
3 answers

You can use DecimalFormat as it even supports locales (in some places it is used . Instead , )

 String number = "1000500000.574"; double amount = Double.parseDouble(number); DecimalFormat formatter = new DecimalFormat("#,###.00"); String formatted = formatter.format(amount); 
+5
source

Effective method:

 private String getFormatedAmount(int amount){ return NumberFormat.getNumberInstance(Locale.US).format(amount); } 

Result:

I / P 10 O / P 10

I / P 100 O / P 100

I / P 1000 to O / P 1,000

I / P 10000 to O / P 10,000

I / P 100000 to O / P 1,00,000

I / P 1000000 to O / P 10,00,000

Hope this helps you.

+9
source

You can implement TextWatcher

 public class NumberTextWatcher implements TextWatcher { private DecimalFormat df; private DecimalFormat dfnd; private boolean hasFractionalPart; private EditText et; public NumberTextWatcher(EditText et) { df = new DecimalFormat("#,###.##"); df.setDecimalSeparatorAlwaysShown(true); dfnd = new DecimalFormat("#,###"); this.et = et; hasFractionalPart = false; } @SuppressWarnings("unused") private static final String TAG = "NumberTextWatcher"; @Override public void afterTextChanged(Editable s) { et.removeTextChangedListener(this); try { int inilen, endlen; inilen = et.getText().length(); String v = s.toString().replace(String.valueOf(df.getDecimalFormatSymbols().getGroupingSeparator()), ""); Number n = df.parse(v); int cp = et.getSelectionStart(); if (hasFractionalPart) { et.setText(df.format(n)); } else { et.setText(dfnd.format(n)); } endlen = et.getText().length(); int sel = (cp + (endlen - inilen)); if (sel > 0 && sel <= et.getText().length()) { et.setSelection(sel); } else { // place cursor at the end? et.setSelection(et.getText().length() - 1); } } catch (NumberFormatException nfe) { // do nothing? } catch (ParseException e) { // do nothing? } et.addTextChangedListener(this); } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { if (s.toString().contains(String.valueOf(df.getDecimalFormatSymbols().getDecimalSeparator()))) { hasFractionalPart = true; } else { hasFractionalPart = false; } } } 

To use it, you can use

 editText.addTextChangedListener(new NumberTextWatcher(editText)); 

Source: Roshka Dev Team

+5
source

Source: https://habr.com/ru/post/1499281/


All Articles