智能转换文件大小单位B,KB,MB,GB
根据文件byte大小,智能转换为合适的大小单位,智能转换文件大小单位B
,KB
,MB
,GB
C#转换大小单位代码:
C# 全选
public static string GetString(long b)
{
const int GB = 1024 * 1024 * 1024;
const int MB = 1024 * 1024;
const int KB = 1024;
if (b / GB >= 1)
{
return Math.Round(b / (float)GB, 2) + "GB";
}
if (b / MB >= 1)
{
return Math.Round(b / (float)MB, 2) + "MB";
}
if (b / KB >= 1)
{
return Math.Round(b / (float)KB, 2) + "KB";
}
return b + "B";
}
JavaScript转换代码:
JavaScript 全选
function ConvertSizeStr(size) {
const GB = 1024 * 1024 * 1024
const MB = 1024 * 1024
const KB = 1024
if (size / GB >= 1) {
return Math.round((size * 100.0) / GB, 2) / 100 + 'GB'
}
if (size / MB >= 1) {
return Math.round((size * 100.0) / MB, 2) / 100 + 'MB'
}
if (size / KB >= 1) {
return Math.round((size * 100.0) / KB, 2) / 100 + 'KB'
}
return size + 'B'
}
版权声明:本文为YES开发框架网发布内容,转载请附上原文出处连接
post 张国生