无法获得小数点后的 2 个值但得到零
unable to get the 2 values after decimals but getting zero
嗨,朋友们,我想得到小数点后的两个值,但在我的小数点后得到 0。这是我下面的代码。
if (!ed.getText().toString().equals("")) {
if (rb1.isChecked()) {
int input = Integer.valueOf(ed.getText().toString());
double out = input / 24;
out = (double)Math.round(out * 100)/100;
Intent m = new Intent(Page.this,MActivity.class);
m.putDoubleExtra("res", out);
startActivity(m);
我还提到了这些网站:Android : How to get just two digit after the point in decimal value ? dont want to trunc the value
Java : how do I get the part after the decimal point?
我也尝试了下面的代码,但它在小数点后得到 0。
DecimalFormat newFormat = new DecimalFormat("#.##");
double twoDecimal = Double.valueOf(newFormat.format(d))
请帮助我编码新手。提前致谢。
由于input
和24
是整数,除以它仍然会产生int
。只有这样它才会被转换成double。
解决此问题的一种方法:double out = input / 24.0;
DecimalFormat newFormat = new DecimalFormat("#.00");
双 twoDecimal = Double.valueOf(newFormat.format(d))
在你的代码中我发现是
out = (double)Math.round(out * 100.0)/100.0;
嗨,朋友们,我想得到小数点后的两个值,但在我的小数点后得到 0。这是我下面的代码。
if (!ed.getText().toString().equals("")) {
if (rb1.isChecked()) {
int input = Integer.valueOf(ed.getText().toString());
double out = input / 24;
out = (double)Math.round(out * 100)/100;
Intent m = new Intent(Page.this,MActivity.class);
m.putDoubleExtra("res", out);
startActivity(m);
我还提到了这些网站:Android : How to get just two digit after the point in decimal value ? dont want to trunc the value
Java : how do I get the part after the decimal point?
我也尝试了下面的代码,但它在小数点后得到 0。
DecimalFormat newFormat = new DecimalFormat("#.##");
double twoDecimal = Double.valueOf(newFormat.format(d))
请帮助我编码新手。提前致谢。
由于input
和24
是整数,除以它仍然会产生int
。只有这样它才会被转换成double。
解决此问题的一种方法:double out = input / 24.0;
DecimalFormat newFormat = new DecimalFormat("#.00"); 双 twoDecimal = Double.valueOf(newFormat.format(d))
在你的代码中我发现是
out = (double)Math.round(out * 100.0)/100.0;