您好,欢迎访问一九零五行业门户网

将字节数转换成用户可读的格式_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=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 $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=
其它类似信息

推荐信息