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

数字转字符串的处理

时间:2017-11-09 22:42:28      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:print   整数   精确   ldd   toc   static   mys   测试   转换   

/**
 * 一些字符串的处理
 *
 * @author ldd
 *
 */
public class MyStringUtils {
    
    /**
     * 提供精确的乘法运算。
     *
     * @param v1 被乘数
     * @param v2 乘数
     * @return 两个参数的积
     */
    public static double mul(double v1, double v2) {
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.multiply(b2).doubleValue();
    }

    /**
     * 将数字转化为大写  
     *
     * @param num
     * @return
     */
    public static String numToUpper(int num) {  
        String u[] = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};  
        char[] str = String.valueOf(num).toCharArray();  
        String rstr = "";  
        for (int i = 0; i < str.length; i++) {  
            rstr = rstr + u[Integer.parseInt(str[i] + "")];
        }  
        return rstr;  
    }
}

//测试
public class Main {

    public static void main(String[] args) throws Throwable {
    //设置一个随意的Double
    Double dou= 10086.01D;
    //先调用精确算法 计算成整数
    String strMoney = MyStringUtils.numToUpper((int)(MyStringUtils.mul(dou, 100))); //人民币数字转换为大写汉字
    System.out.println(strMoney);//壹零零捌陆零壹
    }
}

数字转字符串的处理

标签:print   整数   精确   ldd   toc   static   mys   测试   转换   

原文地址:http://www.cnblogs.com/liudongdong666666/p/7811467.html

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