经常使用的数字方法

  • Math.floor() :   向下取整;
  • Math.ceil()    :   向上取整数;
  • Math.round() :  Math.floor(x+0.5), 即将原来的数字加上0.5后再向下取整。(四舍五入)
  • Math.rint()    :   返回其值最接近参数而且是整数的 double 值。若是两个整数的 double 值都一样接近,那么结果取偶数。特殊状况是:若是参数值是整数,那么结果就是该参数。 若是参数是 NaN 或无穷大或正零或负零,那么结果与参数相同;
  • NumberUtils.isNumber() : 判断字符串是不是数值
    NumberUtils.isNumber("5.96");//结果是true
      NumberUtils.isNumber("5.9.6");//结果是false
      NumberUtils.isNumber("s5");//结果是false
      NumberUtils.isNumber("0000000000596");//结果是true
  • NumberUtils.isDigits() :判断字符串中是否全为数字字符java

    NumberUtils.isDigits("0000000000.596");//false
    NumberUtils.isDigits("0000000000596");//true
  • NumberUtils.max() :找出最大的一个git

  • NumberUtils.min() :找出最小的一个code

    NumberUtils.max(new int[]{3,5,6});//结果是6
    NumberUtils.max(3, 1, 7);//结果是7
    NumberUtils.min(new int[]{3,5,6});//结果是6
    NumberUtils.min(3, 1, 7);//结果是7
  • new BigDecimal(0).setScale(1, BigDecimal.ROUND_HALF_UP):  四舍五入保留1位小数。 orm

  • new BigDecimal(0).subtract(value):  将参数变成负数
  • new BigDecimal(0).stripTrailingZeros().toPlainString() : BigDecimal转String避免科学计数
    BigDecimal totalwgt = new BigDecimal(55553444.012);
    NumberFormat numberFormat = NumberFormat.getNumberInstance();
    numberFormat.setMinimumFractionDigits(1);// 设置小数点后面容许多少位无心义的‘0’
    numberFormat.setGroupingUsed(true);// 设置分组如:‘,’
    String val = numberFormat.format(totalwgt .doubleValue());
    System. out.println(val );// 输出后55,553,444.012
相关文章
相关标签/搜索