1

Here, I have to show currency decimal separator and thousands separators as per the given input like:

    private var decimalSeparator: Char = '.'
    private var thousandSeparator: String = ","

    fun setDecimalSeparator(decimalSeparator: Char) {
        this.decimalSeparator = decimalSeparator
    }

    fun setThousandSeparator(thousandSeparator: String) {
        this.thousandSeparator = thousandSeparator
    }

And I have added text watcher for the edit text field to add a respective decimal and thousands separator like this with the help of Add comma as thousands separator for numbers in EditText for Android Studio?

field.addTextChangedListener(object : TextWatcher {

            
            override fun afterTextChanged(p0: Editable?) {
                Log.d("CharCount:", p0.toString().length.toString())
                field.removeTextChangedListener(this)

                try {
                    var givenstring: String = p0.toString()
                    if (givenstring.contains(thousandSeparator)) {
                        givenstring = givenstring.replace(thousandSeparator.toRegex(), "")
                    }
                    val longVal: Long = givenstring.toLong()
                    val formatter = DecimalFormat("#$thousandSeparator###$thousandSeparator###")
                    val formattedString = formatter.format(longVal)
                    field.setText(formattedString)
                    field.setSelection(field.text.length)
                    // to place the cursor at the end of text
                } catch (nfe: NumberFormatException) {
                    nfe.printStackTrace()
                } catch (e: Exception) {
                    e.printStackTrace()
                }

                field.addTextChangedListener(this)

            }

            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                // no need any callback for this.
            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                // no need any callback for this.
            }

        })

this is not working when thousandSeparator is period(.); can anyone help me with this?. Thanks in advance.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Kiran
  • 31
  • 7
  • can you share the input, current output with the implementation you already have and the expected output? – rbd_sqrl Jan 20 '22 at 04:52
  • @rbd_sqrl the input : text = 1198234,56 thousandSeparator = . decimalSeparator = , expected output will be like = 1.198.234,56 – Kiran Jan 20 '22 at 05:07

2 Answers2

0

Here, this is how it worked out. I took help from https://docs.oracle.com/javase/tutorial/i18n/format/decimalFormat.html and
Add comma as thousands separator for numbers in EditText for Android Studio?

    field.addTextChangedListener(object : TextWatcher {

            // https://stackify.dev/354994-add-comma-as-thousands-separator-for-numbers-in-edittext-for-android-studio
            override fun afterTextChanged(p0: Editable?) {
                field.removeTextChangedListener(this)

                try {
                    var givenstring: String = p0.toString()
                    if (givenstring.contains(thousandSeparator)) {
                        givenstring = givenstring.replace(thousandSeparator.toString(), "")
                    }
                    val doubleVal: Double = givenstring.toDouble()

                    // https://docs.oracle.com/javase/tutorial/i18n/format/decimalFormat.html
                    val unusualSymbols = DecimalFormatSymbols()
                    unusualSymbols.decimalSeparator = decimalSeparator
                    unusualSymbols.groupingSeparator = thousandSeparator

                    val formatter = DecimalFormat("#,##0.##", unusualSymbols)
                    formatter.groupingSize = 3
                    val formattedString = formatter.format(doubleVal)

                    field.setText(formattedString)
                    field.setSelection(field.text.length)
                    // to place the cursor at the end of text
                } catch (nfe: NumberFormatException) {
                    nfe.printStackTrace()
                } catch (e: Exception) {
                    e.printStackTrace()
                }

                field.addTextChangedListener(this)

            }

            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                // no need any callback for this.
            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                // no need any callback for this.
            }

        })
General Grievance
  • 4,555
  • 31
  • 31
  • 45
Kiran
  • 31
  • 7
0

i have made custom text watcher that good for number or currency. You can get it here : https://github.com/zihadrizkyef/TextWatcherForMoney

enter image description here

zihadrizkyef
  • 1,849
  • 3
  • 23
  • 46