zoukankan      html  css  js  c++  java
  • 生成八位不重复编码

    package com.cw.common.util;

    import java.math.BigDecimal;

    public class MathUtil {
    public final static char[] BASE={'0','1','2','3','4','5','6','7','8','9',
    'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};

    public static double keepDecimals(double number, int several){
    BigDecimal ra = BigDecimal.valueOf(number);
    return ra.setScale(several, BigDecimal.ROUND_HALF_UP).doubleValue();
    }


    public synchronized static String generateShortCode(Long number, Integer arbitraryBase) {
    StringBuilder result = new StringBuilder();
    if (number == null){
    number = System.currentTimeMillis();
    }
    if (arbitraryBase == null){
    arbitraryBase = 10;
    }
    if (arbitraryBase > BASE.length){
    throw new IndexOutOfBoundsException("超过进制转换限制,最大"+BASE.length);
    }
    //余数
    int remainder = 0;
    //商
    long quotient;
    //当 商 = 0 时结束
    while ((quotient = number / arbitraryBase) != 0){
    //取余
    remainder = (int)(number % arbitraryBase);
    result.insert(0,BASE[remainder]);

    number = quotient;
    }
    //最后取一次余
    remainder = (int)(number % arbitraryBase);
    result.insert(0,BASE[remainder]);

    int length = result.length();
    if (length < 8){
    for (int i = 0; i < (8 - length); i++) {
    result.insert(0,BASE[0]);
    }
    }
    return result.toString();
    }


    /**
    * @Description: 检测是否在误差范围内
    * @param tag 实际值
    * @param tag2 目标值
    * @param range 误差范围
    * @Return java.lang.Boolean
    * @throws
    * 2020/9/8 16:50
    * @Author milk
    */
    public static Boolean withinRange(double tag, double tag2, double range){
    return Math.abs(tag - tag2) <= Math.abs(range);
    }
    }
  • 相关阅读:
    雑談
    safari下无法模拟click()的解决方法 ------转载
    用户操作
    居中
    replace 重写
    JS数组添加元素的三种方式
    Asp.Net Core 包
    CSS3 动画 可以参考
    CSS3 动画
    完全居中
  • 原文地址:https://www.cnblogs.com/cw828/p/14371423.html
Copyright © 2011-2022 走看看