格式化存储相关的数值时,可以用 android.text.format.Formatter
。
源码说明,在 Android O 后,存储单位的进制是 1000 ,Android N 之前单位进制是 1024 。
/**
* Formats a content size to be in the form of bytes, kilobytes, megabytes, etc.
*
* 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.
*
* 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.
*
* 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.)
*
* @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);
}
源码说明,
/**
* 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);
}