当我在editText中键入时,我想在EditText中使用千位分隔符(,).
之后,我将对数字进行一些操作.然后用千位分隔符(,)在TextView中显示结果.
这是我的代码:
public class Mainactivity extends Activity {
/** Called when the activity is first created. */
EditText edt;
TextView txt;
Button btn;
OnClickListener myclick = new OnClickListener() {
@Override
public void onClick(View arg0) {
// my calculation
double num1 = Double.parseDouble(edt.getText().toString());
double num2 = num1 + 7;
txt.setText("" + num2);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
edt = (EditText) findViewById(R.id.edt);
txt = (TextView) findViewById(R.id.txt);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(myclick);
}
}
解决方法:
您可以使用
String value=String.format("%,.2f", num );
使用千位分隔符将数字解析为String,然后将结果设置为所需的TextView或EditText
例如 :
txt.setText(String.format("%,.2f", num ));
edt.setText(String.format("%,.2f", num ));