MathUtil.java 4.07 KB
package com.subsidy.util;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Random;

public class MathUtil {

    public static String getRandomCode(int randomLength) {
        StringBuilder str = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < randomLength; i++) {
            str.append(random.nextInt(10));
        }
        return str.toString();
    }

    public static String getRandomString(int length) {
        StringBuilder val = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < length; ++i) {
            String charOrNum = random.nextInt(3) % 3 == 0 ? "num" : "char";
            if ("char".equalsIgnoreCase(charOrNum)) {
                int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
                val.append((char) (random.nextInt(26) + temp));
            } else {
                val.append(random.nextInt(10));
            }
        }
        return val.toString();
    }

    //习题/视频 ==> 秒钟向上取整
    public static String ceilSecond(int length) {
        return String.valueOf((int) Math.ceil((double) length / 60));
    }

    //视频秒数处理
    public static String vodLength(int i) {
        if (i > 60) {
            if (i % 60 >= 10) {
                return i / 60 + ":" + i % 60;
            } else {
                return i / 60 + ":0" + i % 60;
            }
        } else {
            if (i % 60 >= 10) {
                return "00:" + i;
            } else {
                return "00:0" + i;
            }
        }
    }

    //文件大小处理
    public static String fileUnit(int i) {
        if (i / 1024 > 1024) {
            return String.format("%.2f", (double) i / 1024 / 1024) + "M";
        } else {
            return String.format("%.2f", (double) i / 1024) + "K";
        }
    }

    //单个题目秒数处理
    public static String exerciseLength(int i) {
        if (i >= 60 * 60) {
            return i / 60 / 60 + "时" + (i - 60 * 60) / 60 + "分" + i % 60 + "秒";
        } else if (i >= 60) {
            return i / 60 + "分" + i % 60 + "秒";
        } else {
            return i % 60 + "秒";
        }
    }

    // 除法取整
    public static String getAvgIndex(BigDecimal counts, BigDecimal total) {
        if (BigDecimal.ZERO.equals(total)) {
            return "0";
        } else {
            return counts.divide(total, RoundingMode.CEILING).stripTrailingZeros().toPlainString();
        }
    }

    //百分比  取整  返回字符串
    public static String getPercentAvgIndex(BigDecimal counts, BigDecimal total) {
        if (BigDecimal.ZERO.equals(total) || BigDecimal.ZERO.equals(counts)) {
            return "0%";
        } else {
            return counts.multiply(new BigDecimal(100)).divide(total, RoundingMode.CEILING).stripTrailingZeros().toPlainString() + "%";
        }
    }

    //百分比  不带%输出
    public static String getPercentAvgIndexWithPercent(BigDecimal counts, BigDecimal total) {
        if (BigDecimal.ZERO.equals(total) || BigDecimal.ZERO.equals(counts)) {
            return "0";
        } else {
            return counts.multiply(new BigDecimal(100)).divide(total, RoundingMode.CEILING).stripTrailingZeros().toPlainString();
        }
    }


    //习题序号生成规则  sid 从数据取出某科目某知识点的最大的序号
    public static String getExeCode(String sid) {
        Integer integer = Integer.valueOf(sid.substring(9));
        return sid.substring(0, 9) + (++integer);
    }

    // 整型a  整型b   a/b  向上取整
    public static Integer intDivCeil(long a, int b) {
        return (int) Math.ceil((double) a / b);
    }

    // 整型a  整型b   a/b  向下取整
    public static Integer intDivFloorPercent(long a, int b) {
        return (int) Math.floor((double) a * 100 / b);
    }

    public static Integer getRandomNum(Integer i){
        Random random = new Random();
        return random.nextInt(i);
    }

//    public static void main(String[] args) {
//        for (int i= 1;i<100;i++){
//            System.out.println(getRandomNum(2));
//        }
//    }
}