java实战 数字处理

Last updated on November 22, 2024 pm

🧙 Questions

☄️ Ideas

数字截取

public void demo() {

    double num = 999.999;
    log.info("num ===> " + num);

    // 截取小数点2位
    DecimalFormat decimalFormat = new DecimalFormat("#.00");
    decimalFormat.setRoundingMode(RoundingMode.DOWN);
    String numStr = decimalFormat.format(num);
    log.info("截取小数点2位 ===> " + numStr);

    // 取整数
    decimalFormat = new DecimalFormat("#");
    decimalFormat.setRoundingMode(RoundingMode.DOWN);
    numStr = decimalFormat.format(num);
    log.info("取整数 ===> " + numStr);
}

随机验证码

public void demo() {

      int codeLength = 6;
      int templateNum = 1;
      while (codeLength > 1) {
          templateNum = templateNum * 10;
          codeLength--;
      }

      log.info("随机数:" + String.valueOf((int) ((Math.random() * 9 + 1) * templateNum)));
  }

版本号

String versionStr = "v4.2.9";
StringBuilder versionResult = new StringBuilder("v");

String[] split = versionStr.split("\\.");
String firstNum = split[0].split("v")[1];
String secondNum = split[1];
String thirdNum = split[2];

if ("9".equals(thirdNum) && "9".equals(secondNum)) {
    versionResult.append(Integer.parseInt(firstNum) + 1).append(".0.0");
} else if ("9".equals(thirdNum)) {
    versionResult.append(firstNum).append(".").append(Integer.parseInt(secondNum) + 1).append(".0");
} else {
    versionResult.append(firstNum).append(".").append(secondNum).append(".").append(Integer.parseInt(thirdNum) + 1);
}

System.out.println("now version:" + versionResult.toString());

随机数


java实战 数字处理
https://ispong.isxcode.com/spring/java/java实战 数字处理/
Author
ispong
Posted on
June 23, 2021
Licensed under