2011-12-27 64 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 
相关问题