Question

I'm trying to create a regular expression for my android app so that currency is formatted as in the top answer to this question:

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]", "");
        StringBuilder cashAmountBuilder = new StringBuilder(userInput);

        while (cashAmountBuilder.length() > 3 && cashAmountBuilder.charAt(0) == '0') {
            cashAmountBuilder.deleteCharAt(0);
        }
        while (cashAmountBuilder.length() < 3) {
            cashAmountBuilder.insert(0, '0');
        }
        cashAmountBuilder.insert(cashAmountBuilder.length()-2, '.');
        cashAmountBuilder.insert(0, '$');

        cashAmountEdit.setText(cashAmountBuilder.toString());
    }

}

Android Money Input with fixed decimal

I want to be able to have the same format as that example achieves just minus the dollar symbol before the numbers, but I'm really unsure about how to alter that code to achieve it, or is there an alternative way?

Edit: After more comments its apparent to me it might not be this particular piece of my code. I can get this working using a very silly workout by changing the dollar sign for a blank space in the regular expression and entering a blank space before the numbers, trimming it later when I need the value, but I'm unable to find a better work around.

private TextWatcher billWatcher = new TextWatcher() {
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

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]", "");
            StringBuilder cashAmountBuilder = new StringBuilder(userInput);

            while (cashAmountBuilder.length() > 3 && cashAmountBuilder.charAt(0) == '0') {
                cashAmountBuilder.deleteCharAt(0);
            }
            while (cashAmountBuilder.length() < 3) {
                cashAmountBuilder.insert(0, '0');
            }
            cashAmountBuilder.insert(cashAmountBuilder.length()-2, '.');
            cashAmountBuilder.insert(0, '$');     

            billBox.setText(cashAmountBuilder.toString());
            billBox.setTextKeepState(cashAmountBuilder.toString());
            Selection.setSelection(billBox.getText(), cashAmountBuilder.toString().length());
        }
    }

XML for the box

 <EditText
        android:id="@+id/billBox"
        android:layout_width="152dp"
        android:layout_height="40dp"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/billText"
        android:layout_marginRight="5dp"
        android:layout_marginTop="5dp"
        android:digits="0123456789."
        android:gravity="right"
        android:inputType="numberDecimal" />
Was it helpful?

Solution

You have: <EditText ... android:digits="0123456789." ... />

and yet your regular expression has \\$(\\d{1,3}.... Which translates to "Dollar sign followed by more digits."

I think the system is confused that you're requiring that a dollar sign be present, but prohibiting it in your XML.

I would take out the first part of your regular expression and put the dollar sign in for the user.

OTHER TIPS

Remove this line

cashAmountBuilder.insert(0, '$');

All you need to do to take out the dollar symbol is take out this line: cashAmountBuilder.insert(0, '$');.

I think you are trying to do too much that the system already can do for you. If you maintain the currency value as a double internally and only format it when you want to display it, it is much easier. See my solution in this link. It may help. currency edit question answer

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top