2011-12-27 66 views
3

如何獲得小數點後只有兩位數的double值。Android:如何獲得小數點後的兩位數?不想截斷值

例如,如果A = 190253.80846153846 那麼結果值應該像= 190253.80

嘗試: 我已經嘗試這樣的:

public static DecimalFormat twoDForm = new DecimalFormat("#0.00"); 

代碼

a = Double.parseDouble(twoDForm.format(((a)))); 

但我得到了像190253.81那樣的價值,而不是我想要的190253.80

那麼我應該改變它呢?

回答

6

因爲Math.round()返回最接近int參數。結果通過加1/2來舍入到一個整數,取結果的底部,並將結果轉換爲int類型。 使用Math.floor()

public static double roundMyData(double Rval, int numberOfDigitsAfterDecimal) { 
        double p = (float)Math.pow(10,numberOfDigitsAfterDecimal); 
       Rval = Rval * p; 
       double tmp = Math.floor(Rval); 
       System.out.println("~~~~~~tmp~~~~~"+tmp); 
       return (double)tmp/p; 
       } 

完整的源代碼

class ZiggyTest2{ 


     public static void main(String[] args) { 
      double num = 190253.80846153846; 
       double round = roundMyData(num,2); 
       System.out.println("Rounded data: " + round); 
       } 

       public static double roundMyData(double Rval, int numberOfDigitsAfterDecimal) { 
        double p = (float)Math.pow(10,numberOfDigitsAfterDecimal); 
       Rval = Rval * p; 
       double tmp = Math.floor(Rval); 
       System.out.println("~~~~~~tmp~~~~~"+tmp); 
       return (double)tmp/p; 
       } 
      } 
+0

由於它的偉大工程。而且我也可以將該功能用於其他目的。 –

2
Following code works for me.  
public static double round(double value, int places) { 
      //here 2 means 2 places after decimal 
      long factor = (long) Math.pow(10, 2); 
      value = value * factor; 
      long tmp = Math.round(value); 
      return (double) tmp/factor; 
     } 
5

試試這個,

使BigDecimal的對象

double a = 190253.80846153846; 
BigDecimal bd = new BigDecimal(a); 
BigDecimal res = bd.setScale(2, RoundingMode.DOWN); 
System.out.println("" + res.toPlainString()); 
+0

這個ans爲我工作:) –

3

由於沒有庫:

a = (float) (((int)(a * 100))/100.0f); 

,或者雙:

a = (double) (((int)(a * 100))/100.0); 
1
double dValue = 10.12345; 
try{ 
    String str = Double.toString(dValue*100);` 
    str = str.split("[.]")[0]; 
    dValue = Double.parseDouble(str)/100; 
} catch (Exception e) { 
    e.printStackTrace(); 
} 
System.out.println(dValue); 

使用此代碼。你可以得到你想要的輸出。

1

這是另一種寫法,它類似於Shabbir's ^的方式,但我認爲更容易閱讀。我其實想要圍繞它;如果它表現出0.349,我想看看0.35 - 但如果你不希望出現這種情況,那麼你可以在我的地方的Math.round使用Math.floor:

public static double round(double time){ 
    time = Math.round(100*time); 
    return time /= 100; 
} 

這裏是我的完整代碼:I我正在研究一個程序,在給定的時間段內找到3個隨機1分鐘的時間間隔,用於正在研究的一個研究項目。

import java.lang.*; 
import java.util.*; 

class time{ 

public static void main (String[] args){ 

    int time = Integer.parseInt(args[0]); 
    System.out.println("time is: "+time); 
    //randomly select one eligible start time among many 
    //the time must not start at the end, a full min is needed from times given 
    time = time - 1; 
    double start1 = Math.random()*time; 
    System.out.println(start1); 
    start1 = round(start1); 
    System.out.println(start1); 

} 

public static double round(double time){ 
    time = Math.round(100*time); 
    return time /= 100; 
} 

} 

來運行它,電流輸出:

[[email protected] edu]$ java time 12 
time is: 12 
10.757832858914 
10.76 

[[email protected] edu]$ java time 12 
time is: 12 
0.043720864837211715 
0.04 
相關問題