使十进制数的百分比

时间:2015-06-05 00:38:40

标签: java android

嘿伙计们,我有问题制作双倍数的percentege。

    double pom = cumulativeProbability(GetZ(baby));

    Log.d("Pom","" + pom);

    double pom1 = round(cumulativeProbability(GetZ(baby)),2);
    Log.d("Pom1", "" + pom1);

    double pom2 = pom1 * 100;

    Log.d("Pom2: ", "" + pom2);

以下是日志:

   06-05 02:30:04.574  13208-13208/com.bojan.readfromcsv D/Pom﹕ 0.5821510290598115
   06-05 02:30:04.584  13208-13208/com.bojan.readfromcsv D/Pom1﹕ 0.58
   06-05 02:30:04.584  13208-13208/com.bojan.readfromcsv D/Pom2:﹕ 57.99999999999999

我想将这个给定的pom值舍入为两位小数,并且我得到了这个舍入函数:

   public static double round(double value, int places) {
    if (places < 0) throw new IllegalArgumentException();

    BigDecimal bd = new BigDecimal(value);
    bd = bd.setScale(places, RoundingMode.HALF_UP);
    return bd.doubleValue();
}

对于最终结果pom2我想要58%.... Tnx

3 个答案:

答案 0 :(得分:0)

尝试:

double pom2 = round(pom1 * 100, 0);

编辑:

int pom2 = (int) round(pom1 * 100, 0);

答案 1 :(得分:0)

我认为你不必编写自己的圆函数,所以为什么不使用:

pom2 = (int) Math.round(pom1 * 100);

答案 2 :(得分:0)

希望这会有所帮助:

public class t {
  public static void main(String args[]) {
      double pom = 0.5821510290598115;
      System.out.println("pom = "+pom);
      double pom1 = Math.round(pom*100)/100.0;
      System.out.println("pom1 = "+pom1);
      System.out.println("pom2 = "+Math.round(pom1*100));
  }
}

产生:

$ java t
pom = 0.5821510290598115
pom1 = 0.58
pom2 = 58

如果你想在pom2中保留更多数字,比如2,请执行以下操作:

System.out.println("pom2 = "+Math.round(pom1*100*100)/100.0);