数值优化展示效果

import java.text.DecimalFormat;

/**
 *  数值处理
 **/
public class NumberUtils {


    /**
     * ① 万以下显示具体数字
     * ② 万以上,亿以下,显示“ X.X — XXXX.X 万”,保留小数点后1位,千位逗号区隔
     * ③ 亿以上显示“ X.X 亿”,保留小数点后1位
     * @param count
     * @return
     */

    public static String getCountCnShow(long count) {
        if (count <= 0)
            return "-";
        String showCnt = "";
        DecimalFormat df = new DecimalFormat("#0.0");
        if (count >= 10000 && count < 100000000) {
            showCnt = String.valueOf(df.format((count * 1.0) / 10000));
//            showCnt = formatZero(showCnt);
            return showCnt + "万";

        } else if (count >= 100000000) {
            showCnt = String.valueOf(df.format((count * 1.0) / 100000000));
            return showCnt + "亿";
        } else {
            showCnt = String.valueOf(count);
            return showCnt;
        }
    }

    /**
     * 对数字进行去除0处理,去除小数点后多余的0,如果都是0的话还需要去除小数点。
     *
     * @param number
     * @return
     */
    public static String formatZero(String number) {
        if (number != null && number.indexOf(".") > 0) {
            // 正则表达
            number = number.replaceAll("0+?$", "");// 去掉后面无用的零
            number = number.replaceAll("[.]$", "");// 如小数点后面全是零则去掉小数点
        }
        return number;
    }

    /**
     * Integer对象判断转换,为null赋默认值0
     * @param val
     * @return
     */
    public static Integer getIntegerOrDefault0(Integer val) {
        return val != null ? val : 0;
    }

    public static void main(String[] args) {
        System.out.println(getCountCnShow(119000000));

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值