• NumberUtils


    package cn.edu.hbcf.common.utils;
    
    import java.math.BigDecimal;
    import java.text.NumberFormat;
    import java.util.regex.Pattern;
    
    
    public class NumberUtils {
    
        /**
         * 处理Double类型数据,如果为空返回0,否则返回其值
         * 
         * @param d
         * @return
         */
        public static double getDoubleNumber(Double d) {
    
            if (d != null) {
                return d.doubleValue();
            } else {
                return 0;
            }
    
        }
        /**
         * 判断 String 类型的数据是否能Double转换
         * @param s
         * @return
         */
        public static boolean isNumString(String s){
             Pattern pattern = Pattern.compile("^[-+]?\d+(\.\d+)?");
             //Pattern pattern = Pattern.compile("(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))");
             
             return pattern.matcher(s).matches();
        }
    
        /**
         * 获取百分数,保留2为小数
         * 
         * @param d
         * @return
         */
        public static String getPercentNumber(Double d) {
            if(d==null){
                d=0.0;
            }
            // 获取格式化对象
            NumberFormat nt = NumberFormat.getPercentInstance();
            // 设置百分数精确度2即保留两位小数
            nt.setMinimumFractionDigits(2);
            return nt.format(d);
        }
        /**
         * 
         * @param d
         * @param a
         * @return
         */
        public static double getDoubleFormat(Double d,int a){
            if(d==null){
                d=0.0;
            }
              BigDecimal bg = new BigDecimal(d);
              return bg.setScale(a, BigDecimal.ROUND_HALF_UP).doubleValue();
        }
    
        /**
         * 保留小数
         * 
         * @param value
         *            位数
         * @return
         */
        public static double toFixed(double value) {        
                long lg = Math.round(value * 100); // 四舍五入
                double d = lg / 100.0; // 注意:使用 100.0而不是 100
                return d;        
        }
    
        public static double getFromPrecentNumber(String s) {
            if (s != null && !"".equals(s)) {
                double d = new Double(s.substring(0, s.indexOf('%'))) / 100.0;
                return d;
            } else
                return 0;
        }
        /**
         * 数值类型转化Double to Int
         * @param d
         * @return
         */
        public static int toInt(Double d){
            if(d == null){
                return 0;
            }else{
                return (int)Math.round(d.doubleValue());
            }
        }
        
        public static String convertDouble(Double d){
            if(d == null){
                return "0";
            }else{
                NumberFormat nf = NumberFormat.getInstance();
                nf.setGroupingUsed(false);
                
                return nf.format(d);
            }
        }
        
        /** 
         * 数字金额大写转换,思想先写个完整的然后将如零拾替换成零 
         * 要用到正则表达式 
         */
        public static String digitUppercase(double n){ 
            String fraction[] = {"角", "分"}; 
            String digit[] = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" }; 
            String unit[][] = {{"元", "万", "亿"}, 
                         {"", "拾", "佰", "仟"}}; 
      
            String head = n < 0? "负": ""; 
            n = Math.abs(n); 
              
            String s = ""; 
            for (int i = 0; i < fraction.length; i++) { 
                s += (digit[(int)(Math.floor(n * 10 * Math.pow(10, i)) % 10)] + fraction[i]).replaceAll("(零.)+", ""); 
            } 
            if(s.length()<1){ 
                s = "整";     
            } 
            int integerPart = (int)Math.floor(n); 
      
            for (int i = 0; i < unit[0].length && integerPart > 0; i++) { 
                String p =""; 
                for (int j = 0; j < unit[1].length && n > 0; j++) { 
                    p = digit[integerPart%10]+unit[1][j] + p; 
                    integerPart = integerPart/10; 
                } 
                s = p.replaceAll("(零.)*零$", "").replaceAll("^$", "零") + unit[0][i] + s; 
            } 
            return head + s.replaceAll("(零.)*零元", "元").replaceFirst("(零.)+", "").replaceAll("(零.)+", "零").replaceAll("^整$", "零元整"); 
        } 
    
    }
  • 相关阅读:
    设置文本框的九种对齐方式(左上,中上,右上,左中,中中,右中,左下,中下,右下)
    VB实现小数和分数的相互转化
    已知三角形三个边的长度值,求三个角的大小
    全国专业技术人员计算机应用能力考试
    EXCELSHEET 中"输入”或“编辑”状态与“就绪”状态的切换
    递归方法巧解不定方程(二)
    VB计算圆周率
    获取路径名的原始大小写状态
    opengl NeNe 第二课的学习
    广东电信公话业务中CRM系统的研究与探索
  • 原文地址:https://www.cnblogs.com/zrui-xyu/p/4942816.html
Copyright © 2020-2023  润新知