PHP與C#分別格式化文件大小的代碼
更新時(shí)間:2011年05月14日 15:40:52 作者:
發(fā)現(xiàn)了一個格式化文件大小的方法, 很帥, 很簡潔, 尤其是 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];
}
復(fù)制代碼 代碼如下:
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# 版:
復(fù)制代碼 代碼如下:
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];
}
相關(guān)文章
Windows下wamp php單元測試工具PHPUnit安裝及生成日志文件配置方法
這篇文章主要介紹了Windows下wamp php單元測試工具PHPUnit安裝及生成日志文件配置方法,簡明扼要的分析了Windows環(huán)境下wamp中php單元測試工具PHPUnit的安裝步驟、操作注意事項(xiàng)以及生成日志文件配置方法,需要的朋友可以參考下2018-05-05
php使用str_replace實(shí)現(xiàn)輸入框回車替換br的方法
這篇文章主要介紹了php使用str_replace實(shí)現(xiàn)輸入框回車替換br的方法,可實(shí)現(xiàn)使用\\n替換成br的方法,需要的朋友可以參考下2014-11-11
PHP函數(shù)實(shí)現(xiàn)從一個文本字符串中提取關(guān)鍵字的方法
這篇文章主要介紹了PHP函數(shù)實(shí)現(xiàn)從一個文本字符串中提取關(guān)鍵字的方法,涉及php針對字符串的遍歷與查找等操作技巧,需要的朋友可以參考下2015-07-07
PHP將整數(shù)數(shù)字轉(zhuǎn)換為羅馬數(shù)字實(shí)例分享
本篇文章中小編給大家分享了一篇關(guān)于PHP將整數(shù)數(shù)字轉(zhuǎn)換為羅馬數(shù)字的知識點(diǎn)以及實(shí)例內(nèi)容,需要的朋友們學(xué)習(xí)下。2019-03-03
如何修改和添加Apache的默認(rèn)站點(diǎn)目錄
本篇文章是對修改和添加Apache的默認(rèn)站點(diǎn)目錄方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-07-07
PHP小偷程序的設(shè)計(jì)與實(shí)現(xiàn)方法詳解
這篇文章主要介紹了PHP小偷程序的設(shè)計(jì)與實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了php基于HTML解析類實(shí)現(xiàn)小偷程序抓取圖片的相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2016-10-10
PHP+MySQL實(shí)現(xiàn)無極限分類欄目的方法
這篇文章主要介紹了PHP+MySQL實(shí)現(xiàn)無極限分類欄目的方法,涉及php操作數(shù)據(jù)庫查詢及結(jié)果集遞歸遍歷的技巧,需要的朋友可以參考下2015-12-12

