格式价格0.00格式使用的EditText机器人格式、机器人、价格、EditText

2023-09-07 15:13:02 作者:栗色゛流年末╮

我试图掩盖价格值,使得它总是在 0.00 格式。这是我的code

I am trying to mask a price value so that it is always in 0.00 format. Here is my code

float temp = 0;

DecimalFormat mDecimalFormat = new DecimalFormat("###.00");

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

    if(s.toString()!=null)
    {

        mMasEditText.removeTextChangedListener(this);
        temp = Float.parseFloat(s.toString());
        mMasEditText.setText(""+mDecimalFormat.format(addNumber(temp)));
        mMasEditText.setSelection(start+1);
        mMasEditText.addTextChangedListener(this);

    }
}

public float addNumber(float numTemp){
    float result=0.00f;
    result = numTemp + result;
    return result;
}

但是,当我pressing小数,我希望光标去一个步骤。但我没能得到点回调。此外,当我pressing后退按钮,以消除数字,我得到一个索引出界异常。谁能告诉我怎么去onTextChanged听众面前后退按钮和圆点按钮回调?

But when I am pressing decimal, I want the cursor to go to one more step. But I am not able to get the dot callback. Also when I am pressing the back button, to remove digits, I get an index out of bound exception. Can anyone tell me how to get the back button and dot button callback before onTextChanged listener?

推荐答案

使用的String.Format

  String.format("$%.2f", result);

   Strint text = String.format("$%.2f", addNumber(temp));
   mMasEditText.setText(text)