Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Better way to Format Currency Input editText?

Tags:

android

I have an editText, starting value is $0.00. When you press 1, it changes to $0.01. Press 4, it goes to $0.14. Press 8, $1.48. Press backspace, $0.14, etc.

That works, the problem is, if somebody manually positions the cursor, problems occur in the formatting. If they were to delete the decimal, it won't come back. If they put the cursor in front of the decimal and type 2, it will display $02.00 instead of $2.00. If they try to delete the $ it will delete a digit instead, for example.

Here is code I'm using, I'd appreciate any suggestions.

mEditPrice.setRawInputType(Configuration.KEYBOARD_12KEY);     public void priceClick(View view) {     mEditPrice.addTextChangedListener(new TextWatcher(){         DecimalFormat dec = new DecimalFormat("0.00");         @Override         public void afterTextChanged(Editable arg0) {         }         @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().matches("^\\$(\\d{1,3}(\\,\\d{3})*|(\\d+))(\\.\\d{2})?$"))             {                 String userInput= ""+s.toString().replaceAll("[^\\d]", "");                 if (userInput.length() > 0) {                     Float in=Float.parseFloat(userInput);                     float percen = in/100;                     mEditPrice.setText("$"+dec.format(percen));                     mEditPrice.setSelection(mEditPrice.getText().length());                 }             }         }     }); 
like image 440
Roger Avatar asked Feb 24 '11 17:02

Roger


1 Answers

I tested your method, but it fails when I use great numbers... I created this:

private String current = ""; @Override public void onTextChanged(CharSequence s, int start, int before, int count) {     if(!s.toString().equals(current)){        [your_edittext].removeTextChangedListener(this);         String cleanString = s.toString().replaceAll("[$,.]", "");                         double parsed = Double.parseDouble(cleanString);        String formatted = NumberFormat.getCurrencyInstance().format((parsed/100));                             current = formatted;        [your_edittext].setText(formatted);        [your_edittext].setSelection(formatted.length());                [your_edittext].addTextChangedListener(this);     } } 

Kotlin variant:

private var current: String = ""           override fun onTextChanged(             s: CharSequence,             start: Int,             before: Int,             count: Int         ) {             if (s.toString() != current) {                 discount_amount_edit_text.removeTextChangedListener(this)                  val cleanString: String = s.replace("""[$,.]""".toRegex(), "")                  val parsed = cleanString.toDouble()                 val formatted = NumberFormat.getCurrencyInstance().format((parsed / 100))                  current = formatted                 discount_amount_edit_text.setText(formatted)                 discount_amount_edit_text.setSelection(formatted.length)                  discount_amount_edit_text.addTextChangedListener(this)             }         } 
like image 147
Guilherme Oliveira Avatar answered Oct 04 '22 15:10

Guilherme Oliveira