将字节数转换成用户可读的格式_2
=Start=
接上篇「 将字节数转换成用户可读的格式」,上篇文章主要是使用Linux下已有的工具(numfmt,需要 GNU coreutils >= 8.21)进行转换,但是我记录这篇文章的最初目的是自己编码实现相关功能(比如写成一个alias/function放在.bashrc中方便日常使用),这篇文章的内容就是介绍通过各种编程语言来实现该功能。
参考解答:
1.awk/gawk
# OKecho "12454162221" | awk ' BEGIN { split("B,KB,MB,GB,TB", Units, ","); } { u = 1; while ($1 >= 1024) { $1 = $1 / 1024; u += 1 } $1 = sprintf("%.2f %s", $1, Units[u]); print $0; }' # OKecho "12454162221" | gawk 'BEGIN { split("KMGTPEZY",suff,//)}{ match($0,/([0-9]+)/,bits) sz=bits[1]+0 i=0; while ((sz>1024)&&(i<length(suff))) { sz/=1024;i++ } if (i) printf("%.3f %siB\n",sz,suff[i]) else printf("%3i B\n",sz)}' # OKecho "12454162221" | awk '{ xin=$1; if(xin==0) { print "0 B"; } else { x=(xin<0?-xin:xin); s=(xin<0?-1:1); split("B KiB MiB GiB TiB PiB",type); for(i=5;y < 1;i--) { y=x/(2^(10*i)); } print y*s " " type[i+2]; };}'
2.Perl
echo "12454162221" | perl -ne 'if (/^(\d+)/){$l=log($1+.1);$m=int($l/log(1024)); printf("%6.1f\t%s\n",($1/(2**(10*$m))),("K","M","G","T","P")[$m-1]);}' # 最大以G为单位echo "12454162221" | perl -pe 's{([0-9]+)}{sprintf "%.1f%s", $1>=2**30? ($1/2**30, "G"): $1>=2**20? ($1/2**20, "M"): $1>=2**10? ($1/2**10, "K"): ($1, "")}e'
3.Python
defbytes_format(filesize, unit=1024): unit = float(unit) for countin ['Bytes','KB','MB','GB', 'TB', 'PB']: if 0 < filesize < unit: return '{0:.3f} {1}'.format(filesize, count) filesize /= unit printbytes_format(12454162221)
4.PHP
<?php function bytes_format($numbers, $bytesize = 1024) { $readable = array("", "KB", "MB", "GB", "TB", "PB"); $index = 0; while($numbers > $bytesize){ $numbers /= $bytesize; $index++; } return("".round($numbers, 2)." ".$readable[$index]);}echobytes_format(12454162221) . "\n";echobytes_format(124541622210) . "\n";echobytes_format(1245416222100) . "\n";
参考链接:
- http://unix.stackexchange.com/questions/44040/a-standard-tool-to-convert-a-byte-count-into-human-kib-mib-etc-like-du-ls1
- http://stackoverflow.com/questions/15854332/file-size-in-human-readable-format
- http://www.kossboss.com/linux—bytes-to-human-readable-command
- http://ram.kossboss.com/humanbytesawk/
- http://ram.kossboss.com/linux-bytes-to-human-readable-command/
- http://superuser.com/questions/553976/how-to-display-the-size-in-human-readable-format-in-the-find-command/554027#554027
- http://serverfault.com/questions/62411/how-can-i-sort-du-h-output-by-size
- http://codesnippets.fesslersoft.de/format-bytes-to-human-readable-size/
- http://www.developerfeed.com/how-convert-bytes-human-readable-string-format-php/
- http://www.ivankristianto.com/tips-convert-your-numbers-to-human-readable-format/
=END=

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

PHP客户端URL(curl)扩展是开发人员的强大工具,可以与远程服务器和REST API无缝交互。通过利用Libcurl(备受尊敬的多协议文件传输库),PHP curl促进了有效的执行

您是否想为客户最紧迫的问题提供实时的即时解决方案? 实时聊天使您可以与客户进行实时对话,并立即解决他们的问题。它允许您为您的自定义提供更快的服务

文章讨论了PHP 5.3中引入的PHP中的晚期静态结合(LSB),从而允许静态方法的运行时分辨率调用以获得更灵活的继承。 LSB的实用应用和潜在的触摸

JWT是一种基于JSON的开放标准,用于在各方之间安全地传输信息,主要用于身份验证和信息交换。1.JWT由Header、Payload和Signature三部分组成。2.JWT的工作原理包括生成JWT、验证JWT和解析Payload三个步骤。3.在PHP中使用JWT进行身份验证时,可以生成和验证JWT,并在高级用法中包含用户角色和权限信息。4.常见错误包括签名验证失败、令牌过期和Payload过大,调试技巧包括使用调试工具和日志记录。5.性能优化和最佳实践包括使用合适的签名算法、合理设置有效期、

使用PHP的cURL库发送JSON数据在PHP开发中,经常需要与外部API进行交互,其中一种常见的方式是使用cURL库发送POST�...
