码迷,mamicode.com
首页 > 其他好文 > 详细

NumberUtils

时间:2015-11-06 16:02:16      阅读:268      评论:0      收藏:0      [点我收藏+]

标签:

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("^整$", "零元整"); 
    } 

}

 

NumberUtils

标签:

原文地址:http://www.cnblogs.com/zrui-xyu/p/4942816.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!