浮点及字符串转百分比

原创
2017/04/05 13:57
阅读数 1K
    /*
     * 浮点转百分比,保留2位小数
     * 0.123456 -> 12.34%
     */
    public static String floatToPercent(Float num) {
        NumberFormat percentFormat = NumberFormat.getPercentInstance();
        percentFormat.setMaximumFractionDigits(2); //最大小数位数
        return percentFormat.format(num);
    }

    /*
     * 字符串转百分比,保留2位小数
     * 0.123456 -> 12.34%
     */
    public static String floatToPercent(String num) {
        NumberFormat percentFormat = NumberFormat.getPercentInstance();
        percentFormat.setMaximumFractionDigits(2); //最大小数位数
        if (StringUtils.isEmpty(num)) {
            return "";
        }

        float num_float;
        try {
            num_float = Float.parseFloat(num);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
        return percentFormat.format(num_float);
    }

 

展开阅读全文
加载中

作者的其它热门文章

打赏
0
0 收藏
分享
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部