Converts the number of bytes into a user-readable format _2

Source: Internet
Author: User
Tags sprintf
=start=

Next, "Convert bytes to user-readable format", the previous article is mainly using the existing tools (NUMFMT, GNU coreutils >= 8.21) to convert But my original purpose in documenting this article was to encode and implement the relevant functions (such as writing a alias/function in. BASHRC for everyday use), and the content of this article is to introduce this feature through a variety of programming languages.

Reference Answer:

1.awk/gawk

# Okecho "12454162221" |    awk ' BEGIN {split ("B,KB,MB,GB,TB", Units, ",");       } {u = 1;          while ($ >= 1024) {$ = $1/1024;       U + = 1} $ = sprintf ("%.2f%s", $ units[u]);    print $; } ' # Okecho ' 12454162221 ' | Gawk ' BEGIN {split ("Kmgtpezy", suff,//)} {match ($0,/([0-9]+)/,bits) sz=bits[1]+0 i=0; while ((sz>1024) && (i
     
  

2.Perl

echo "12454162221" | Perl-ne ' if (/^ (\d+)/) {$l =log ($1+.1); $m =int ($l/log (1024x768)); printf ("%6.1f\t%s\n", ($1/(2**), 10* $m)), ("K", "M", "G", "t "," P ") [$m-1]);} ' # echo "12454162221" in G as the largest unit | 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"): ($, "")}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_forma T (12454162221)

4.PHP

 
    $bytesize) {      s/= $bytesize;      $index + +;  }  Return ("". Round (s, 2). " ". $readable [$index]);} Echobytes_format (12454162221). "\ n"; Echobytes_format (124541622210). "\ n"; Echobytes_format (1245416222100). "\ n";

Reference Links:

    • http://unix.stackexchange.com/questions/44040/a-standard-tool-to-convert-a-byte-count-into-human-kib-m IB-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=

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.