Exemplo n.º 1
0
  /**
   * 除法运算<br>
   * 当发生除不尽的情况时,由scale参数指定精度,以后的数字四舍五入。
   *
   * @autor:chenssy
   * @date:2014年9月15日
   * @param v1 除数
   * @param v2 被除数
   * @param scale 精确精度
   * @return
   */
  public static String div(String v1, String v2, int scale, int round) {
    if (scale < 0) {
      throw new IllegalArgumentException("The scale must be a positive integer or zero");
    }

    if (ValidateHelper.isEmpty(scale)) {
      scale = DEFAULT_SCALE;
    }

    if (ValidateHelper.isEmpty(round)) {
      round = DEFAULT_ROUND;
    }

    BigDecimal b1 = new BigDecimal(v1);
    BigDecimal b2 = new BigDecimal(v2);
    return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).toString();
  }
Exemplo n.º 2
0
  /**
   * 将object转换为Bigdecimal,若object为空,则返回resultValue
   *
   * @autor:chenssy
   * @date:2014年9月20日
   * @param value
   * @return
   */
  public static BigDecimal getBigDecimal(Object value, BigDecimal resultValue) {
    if (ValidateHelper.isEmpty(value)) {
      return resultValue;
    }

    resultValue = getBigDecimal(resultValue);

    return resultValue;
  }
Exemplo n.º 3
0
 /**
  * 处理BigDecimal数据,保留scale位小数
  *
  * @author:chenssy
  * @date:2014年10月21日
  * @param value
  * @param scale
  * @return
  */
 public static BigDecimal getValue(BigDecimal value, int scale) {
   if (!ValidateHelper.isEmpty(value)) {
     return value.setScale(scale, BigDecimal.ROUND_HALF_UP);
   }
   return value;
 }