我已经使用了十进制格式类来设置editText变量为我的格式变量mark1Format
。但我不知道如何设置新的格式变量到这个我的意思是设置格式变量R.id.mark1Format。任何人都有解决方案吗?如何将双变量格式化为两位小数?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
result1 = (EditText)findViewById(R.id.mark1);
result2 = (EditText)findViewById(R.id.mark2);
Intent intent = getIntent();
double mark1 = intent.getDoubleExtra("number1", 0);
double mark2 = intent.getDoubleExtra("number2", 0);
//format to two decimal places
DecimalFormat df = new DecimalFormat("#.00");
String mark1Format = df.format(mark1);
String mark2Format = df.format(mark2);
//set the variables on EditTexts like this :
result1 = (EditText)findViewById(R.id.mark1);
result2 = (EditText)findViewById(R.id.mark2);
result1.setText(mark1+"");
result2.setText(mark2+"");
}
我的头顶作品没有理由'的DecimalFormat(“# 。##“)'应该可以工作 –
是的,但我不知道如何将editText结果设置为我的格式化结果..任何想法? – user2815899
'result1.setText(df.format(mark1));' –