java计算百分比

2022-11-03  本文已影响0人  writeanewworld
   public static String percent(int x, int y) {
    if (x == 0 || y == 0) {
      return "0";
    }
    double d1 = x * 1.0;
    double d2 = y * 1.0;
    NumberFormat percentInstance = NumberFormat.getPercentInstance();
    // 设置保留几位小数
    percentInstance.setMinimumFractionDigits(0);
    return percentInstance.format(d1 / d2).replace("%", "");
  }
上一篇 下一篇

猜你喜欢

热点阅读