Java Android如何设置点后的纯文本数字
我在计算一个公式。我编写了代码,它工作正常,但我希望看到velocity text的结果如下: 5.28(点后仅2个数字) 明文的Digits属性不适用于我。 我说速度是双精度的 我怎么做 对不起,我的英语很差,谢谢你 编辑: 现在我必须将您的代码放入我的代码中,但我不知道在哪里:)Java Android如何设置点后的纯文本数字,java,android,digits,plaintext,Java,Android,Digits,Plaintext,我在计算一个公式。我编写了代码,它工作正常,但我希望看到velocity text的结果如下: 5.28(点后仅2个数字) 明文的Digits属性不适用于我。 我说速度是双精度的 我怎么做 对不起,我的英语很差,谢谢你 编辑: 现在我必须将您的代码放入我的代码中,但我不知道在哪里:) 我很抱歉这个简单的问题。我真的需要学习很多关于java的话题 您可以将十进制格式化程序用作- NumberFormat formatter = new DecimalFormat("#0.00"); Lo
我很抱歉这个简单的问题。我真的需要学习很多关于java的话题 您可以将十进制格式化程序用作-
NumberFormat formatter = new DecimalFormat("#0.00");
Log.d("formatted no is ",""+formatter.format(4.0));
编辑
您可以像这样修改代码-
public void onButtonClick(View v)
after some formulas
velocity2 = velocity * 0.00508;
drop2 = pressuredrop * 249.174;
vel.setText(formatter.format(velocity2));
dr.setText(formatter.format(drop2));
}
你到底需要在哪里看到文本?你写了一些代码吗?请编辑您的问题并添加代码(即使它不起作用),这样我们就可以找出错误所在。如果您只想打印两位小数(假设您想要四舍五入)<代码>System.out.printf(“%.2f”,num)代码>谢谢你的回复。我在问题中添加了我的代码,您能解释一下我将在哪里添加您的代码吗?
public void onButtonClick(View v)
after some formulas
velocity2 = velocity * 0.00508;
drop2 = pressuredrop * 249.174;
vel.setText(formatter.format(velocity2));
dr.setText(formatter.format(drop2));
}