如何列出最多两位小数的数字?我尝试过这种方法:http://developer.android.com/reference/java/text/NumberFormat.html但没有运气。下面的代码。也许有人可以帮助我。
package karcio.fuel.economy;
public class FuelEconomy extends Activity
{
private EditText editText1;
private EditText editText2;
private TextView textView4;
private TextView textView6;
private Button button1;
private double miles;
private double liters;
private double result;
private double convertMilesToKm;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initParams();
}
private void initParams()
{
editText1 = (EditText)findViewById(R.id.editText1);
editText2 = (EditText)findViewById(R.id.editText2);
textView4 = (TextView)findViewById(R.id.textView4);
textView6 = (TextView)findViewById(R.id.textView6);
button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new Button.OnClickListener()
{
public void onClick (View v)
{
calculate();
}
});
}
private void calculate()
{
miles = Double.parseDouble(editText1.getText().toString());
liters = Double.parseDouble(editText2.getText().toString());
convertMilesToKm = miles * 1.61;
result = 100 * liters / convertMilesToKm;
textView6.setText(Double.toString(convertMilesToKm));
textView4.setText(Double.toString(result));
}
}
答案 0 :(得分:4)
您可以这样做:
String str = String.format("%.2f", 3.99999);
textView.setText(str);
答案 1 :(得分:1)
你可以尝试手动完成它。
//This is just an example
double number = result; //result is YOUR variable (ex. result = 23.1231231241920312)
int tmp = number * 100; //2312.31231241920312
number = (double)tmp / 100; //23.12
希望这有帮助。
注意:如果您在另一行上执行此操作,则可以跳过我声明INT的步骤。
更新:使用此方法的优点是您不需要创建更快的Object,但当然有很多方法。