All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.whyareyousoseriously.czcommonutils.util.DigitalUtil Maven / Gradle / Ivy

package io.github.whyareyousoseriously.czcommonutils.util;

import com.google.common.collect.Lists;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author chenzhen
 * Created by chenzhen on 2020/1/9.
 */
public class DigitalUtil {

    /**
     * 自动单位转换
     * @param l 数字
     * @return 转换后结果
     */
    public static String autoUnitConversion(long l){
        if (l>10000&&l<10000000){
            double l1 = l/1000;
            return String.valueOf(l1).split("\\.")[0]+"万";
        }
        if (l>10000000&l<100000000){
            double l1 = l/10000000;
            return String.valueOf(l1).split("\\.")[0]+"千万";
        }
        if (l>100000000){
            double l1 = l/100000000;
            return String.valueOf(l1).split("\\.")[0]+"亿";
        }
        return ""+l;
    }

    /**
     * 保留n位小数
     * @param d d
     * @param n 保留位数
     * @return double
     */
    public static double roundHalfUp(double d , int n){
        BigDecimal bg = new BigDecimal(d);
        return bg.setScale(n, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    /**
     * 保留n位小数
     * @param d d
     * @param n 保留位数
     * @return double
     */
    public static double roundHalfUp(long d , int n){
        BigDecimal bg = new BigDecimal(d);
        return bg.setScale(n, BigDecimal.ROUND_HALF_UP).doubleValue();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy