2016-05-04 31 views
0

您好朋友我想在小數點後得到兩個值,但在小數點後我得到0。這是我的代碼如下。無法獲得小數點後的2個值,但變爲零

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)) 

請幫我看一下新的編碼。提前致謝。

+0

d的值是多少? –

回答

0

DecimalFormat的newFormat =新DecimalFormat的( 「#00」); 雙twoDecimal = Double.valueOf(newFormat.format(d))

在代碼我發現是

OUT =(雙)Math.round(出* 100.0)/100.0;

+0

我得到的結果僅0 – sun

+0

謝謝@naitik – sun

+0

嘗試使用double input = Double.ValueOf(edittext) – Naitik

1

由於input24是整數,所以在劃分時它仍然會產生int。只有這樣它纔會轉化爲雙倍。解決這個

一種方式:double out = input/24.0;