java / 安卓 把数字格式化为视频/音频中常用的播放量,如 1.2万

版权声明:有需要的请联系QQ1634475153,欢迎技术交流 https://blog.csdn.net/jinmie0193/article/details/82810890

    /**
     * 播放量
     * @param playCount:后台返回的播放量(单位:个)
     * 播放量 < 1万,显示样式 1、10、1000
     * 播放量 ≥ 1万,显示样式 1.2万 1.23万
     * 播放量 ≥ 1亿,显示样式 1.2亿 1.23亿*/


    public static String formatPlayCount(long playCount){
        String standardPlayCount = "";
        if (playCount < 0) {
            standardPlayCount = "0";
        } else if (playCount < 10000) {
            standardPlayCount = String.valueOf(playCount);
        } else if (playCount < 100000000) {
            standardPlayCount = String.format(Locale.getDefault(), "%d.%02d万", playCount / 10000, playCount % 10000 / 100);
        } else if (playCount > 100000000) {
            standardPlayCount = String.format(Locale.getDefault(), "%d.%02d亿", playCount / 100000000, playCount % 100000000 / 1000000);
        }
        return standardPlayCount;
    }

  %02d 就是说长度不够2位的时候前面补0 

猜你喜欢

转载自blog.csdn.net/jinmie0193/article/details/82810890