How to Automatically add thousand separators as number is input in EditText

后端 未结 13 2060
花落未央
花落未央 2020-11-27 05:21

Im creating a convertor application, I want to set the EditText so that when the user is inputting the number to be converted, a thousand separator (,) should be added autom

13条回答
  •  臣服心动
    2020-11-27 05:40

    you can use this code in many ways in your program, you give it a string and it separate each three from right and place space there.

    private String Spacer(String number){
        StringBuilder strB = new StringBuilder();
        strB.append(number);
        int Three = 0;
    
        for(int i=number.length();i>0;i--){
            Three++;
            if(Three == 3){
                strB.insert(i-1, " ");
                Three = 0;
            }
        }
        return strB.toString();
    }// end Spacer()
    

    u can change it a bit and use it ontextchangelistener. good luck

提交回复
热议问题