您好我是java和Android的新手,我目前正在学习Android作为学校项目。
我如何设置它以使editText框中的值仅设置为2位小数?
感谢您的帮助, 史蒂芬。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Finds varibles Id.
e1 = (EditText) findViewById(R.id.inLow);
e2 = (EditText) findViewById(R.id.inHigh);
e3 = (EditText) findViewById(R.id.outLow);
e4 = (EditText) findViewById(R.id.outHigh);
e5 = (EditText) findViewById(R.id.var);
e7 = (EditText) findViewById(R.id.out);
TextView t8 = (TextView) findViewById(R.id.inText);
TextView t6 = (TextView) findViewById(R.id.outText);
b9 = (Button) findViewById(R.id.con);
initButton();
}
private void initButton() {
b9.setOnClickListener(new View.OnClickListener() {
// this one performs an action when our button is clicked. it performs whatever is below
@Override
public void onClick(View v) {
String strA = e1.getText().toString();
String strB = e2.getText().toString();
String strC = e3.getText().toString();
String strD = e4.getText().toString();
String strE = e5.getText().toString();
String strF = e7.getText().toString();
//Check if value is in editText boxes.
if (e1.getText().length() == 0 || e2.getText().length() == 0 || e3.getText().length() == 0 || e4.getText().length() == 0 || e5.getText().length() == 0 || e7.getText().length() == 0) {
if (e1.getText().length() == 0){
e1.setHint("Enter Value!");
}else if (e2.getText().length() == 0){
e2.setHint("Enter Value!");
}else if (e3.getText().length() == 0){
e3.setHint("Enter Value!");
}else if (e4.getText().length() == 0){
e4.setHint("Enter Value!");
}else if (e5.getText().length() == 0){
e5.setHint("Enter Value!");
}else if (e7.getText().length() == 0){
e7.setHint("Enter Value!");
}
} else {
Double dblAnswer = doCalc(strA, strB, strC, strD, strE, strF);
Double dblAnswer2 = doCalc2(strA, strB, strC, strD, strE, strF);
TextView lblAnswer1 = (TextView) findViewById(R.id.outText);
TextView lblAnswer2 = (TextView) findViewById(R.id.inText);
e2.setText(String.format("%.2f", strA));
e3.setText(String.format("%.2f", 3));
e4.setText(String.format("%.2f", 4));
e5.setText(String.format("%.2f", 5));
e7.setText(String.format("%.2f", 6));
t6.setText(String.format("%.2f", 7));
t8.setText(String.format("%.2f", 8));*/
String answer1 = String.valueOf(dblAnswer);
String answer2 = String.valueOf(dblAnswer2);
lblAnswer1.setText(answer1);
lblAnswer2.setText(answer2);
}
}
});}
public double doCalc(String a, String b, String c, String d,String e, String f ) {
double dblA = Double.parseDouble(a);
double dblB = Double.parseDouble(b);
double dblC = Double.parseDouble(c);
double dblD = Double.parseDouble(d);
double dblE = Double.parseDouble(e);
double dblF = Double.parseDouble(f);
return (((dblD-dblC)/(dblB-dblA))*(dblE-dblA)+dblC);
}
public double doCalc2(String a, String b, String c, String d,String e, String f ) {
double dblA = Double.parseDouble(a);
double dblB = Double.parseDouble(b);
double dblC = Double.parseDouble(c);
double dblD = Double.parseDouble(d);
double dblE = Double.parseDouble(e);
double dblF = Double.parseDouble(f);
return (((dblA-dblB)/(dblC-dblD))*(dblF-dblC))+dblA;
}
答案 0 :(得分:1)
DecimalFormat formatter = new DecimalFormat("00");
String minformat = formatter.format(text);
答案 1 :(得分:0)
使用此选项可以舍入到两位小数
public static double round2(double d) {
return Math.round(d * 100) / 100.0;
}
答案 2 :(得分:0)
NumberFormat课程将帮助您实现这一目标。
NumberFormat nf = NumberFormat.getInstance();
nf.setMinimumFractionDigits(2);
nf.setMaximumFractionDigits(2);
然后使用format()方法设置文本:myTextView.setText(nf.format(myText));
您还可能希望在EditText中将输入格式检查为用户类型。要执行此操作,您应该addTextChangedListener(TextWatcher watcher)并听取用户输入事件。