Android 格式化存储之Formatter

6 篇文章 0 订阅
1 篇文章 0 订阅

格式化存储相关的数值时,可以用 android.text.format.Formatter

Formatter.formatFileSize(Context context, long sizeBytes)

源码说明,在 Android O 后,存储单位的进制是 1000 ,Android N 之前单位进制是 1024 。

/**
     * Formats a content size to be in the form of bytes, kilobytes, megabytes, etc.
     *
     * <p>As of O, the prefixes are used in their standard meanings in the SI system, so kB = 1000
     * bytes, MB = 1,000,000 bytes, etc.</p>
     *
     * <p class="note">In {@link android.os.Build.VERSION_CODES#N} and earlier, powers of 1024 are
     * used instead, with KB = 1024 bytes, MB = 1,048,576 bytes, etc.</p>
     *
     * <p>If the context has a right-to-left locale, the returned string is wrapped in bidi
     * formatting characters to make sure it's displayed correctly if inserted inside a
     * right-to-left string. (This is useful in cases where the unit strings, like "MB", are
     * left-to-right, but the locale is right-to-left.)</p>
     *
     * @param context Context to use to load the localized units
     * @param sizeBytes size value to be formatted, in bytes
     * @return formatted string with the number
     */
    public static String formatFileSize(@Nullable Context context, long sizeBytes) {
        return formatFileSize(context, sizeBytes, FLAG_SI_UNITS);
    }

它采用四舍五入,保留两位小数,自动补全 kB 、MB、GB 等单位。
示例,注释即是运行结果

	private void testFormatFileSize(){
        long size0 = 1000; // 1.00 kB
        long size1 = 1024; // 1.02 kB
        long size2 = 1000 * 1000; // 1.00 MB
        long size3 = 1000 * 1000 * 1000; // 1.00 GB
        long size4 = size3 + 30 * size2; // 1.03 GB
        long size5 = size3 + 519 * size2; //  1.52 GB
        String result0 = Formatter.formatFileSize(MainActivity.this, size0);
        String result1 = Formatter.formatFileSize(MainActivity.this, size1);
        String result2 = Formatter.formatFileSize(MainActivity.this, size2);
        String result3 = Formatter.formatFileSize(MainActivity.this, size3);
        String result4 = Formatter.formatFileSize(MainActivity.this, size4);
        String result5 = Formatter.formatFileSize(MainActivity.this, size5);
        Log.d("test" , "[MainActivity] -- formatFileSize -- result0 : " + result0);
        Log.d("test" , "[MainActivity] -- formatFileSize -- result1 : " + result1);
        Log.d("test" , "[MainActivity] -- formatFileSize -- result2 : " + result2);
        Log.d("test" , "[MainActivity] -- formatFileSize -- result3 : " + result3);
        Log.d("test" , "[MainActivity] -- formatFileSize -- result4 : " + result4);
        Log.d("test" , "[MainActivity] -- formatFileSize -- result5 : " + result5);
    }

Formatter.formatShortFileSize(Context context, long sizeBytes)

源码说明,

/**
     * Like {@link #formatFileSize}, but trying to generate shorter numbers
     * (showing fewer digits of precision).
     */
    public static String formatShortFileSize(@Nullable Context context, long sizeBytes) {
        if (context == null) {
            return "";
        }
        final BytesResult res = formatBytes(context.getResources(), sizeBytes,
                FLAG_SI_UNITS | FLAG_SHORTER);
        return bidiWrap(context, context.getString(com.android.internal.R.string.fileSizeSuffix,
                res.value, res.units));
    }

它采用四舍五入,保留一位小数,自动补全 kB 、MB、GB 等单位。
示例,注释即是运行结果

	private void testFormatShortFileSize(){
        long size0 = 1000; // 1.0 kB
        long size1 = 1024; // 1.0 kB
        long size2 = 1000 * 1000; // 1.0 MB
        long size3 = 1000 * 1000 * 1000; // 1.0 GB
        long size4 = size3 + 256 * size2; // 1.3 GB
        String result0 = Formatter.formatShortFileSize(MainActivity.this, size0);
        String result1 = Formatter.formatShortFileSize(MainActivity.this, size1);
        String result2 = Formatter.formatShortFileSize(MainActivity.this, size2);
        String result3 = Formatter.formatShortFileSize(MainActivity.this, size3);
        String result4 = Formatter.formatShortFileSize(MainActivity.this, size4);
        Log.d("test" , "[MainActivity] -- testFormatShortFileSize -- result0 : " + result0);
        Log.d("test" , "[MainActivity] -- testFormatShortFileSize -- result1 : " + result1);
        Log.d("test" , "[MainActivity] -- testFormatShortFileSize -- result2 : " + result2);
        Log.d("test" , "[MainActivity] -- testFormatShortFileSize -- result3 : " + result3);
        Log.d("test" , "[MainActivity] -- testFormatShortFileSize -- result4 : " + result4);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值