当前位置: 首页 > 新闻动态 > 软件编程

PHP与C#分别格式化文件大小的代码

作者:用户投稿 浏览: 发布日期:2026-01-16
[导读]:发现了一个格式化文件大小的方法, 很帅, 很简洁, 尤其是 PHP 版的, 只需要 2 行代码
PHP 版:
复制代码 代码如下:
function format($size)
{
$sizetext = array(" B", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB");
return round($size/pow(1024,($i=floor(log($size,1024)))),2).$sizetext[$i];
}

C# 版:
复制代码 代码如下:
public string formatSize(long size)
{
if (size == 0) return "0";
string[] sizetext = new string[] { " B", " KB", " MB", " GB", " TB", " PB" };
int i = (int)Math.Floor(Math.Log(size, 1024));
return Math.Round(size / Math.Pow(1024, i), 2).ToString() + sizetext[i];
}
免责声明:转载请注明出处:http://jing-feng.com.cn/news/846244.html

扫一扫高效沟通

多一份参考总有益处

免费领取网站策划SEO优化策划方案

请填写下方表单,我们会尽快与您联系
感谢您的咨询,我们会尽快给您回复!