1Human(3) User Contributed Perl Documentation Human(3)
2
3
4
6 Number::Bytes::Human - Convert byte count to human readable format
7
9 use Number::Bytes::Human qw(format_bytes);
10 $size = format_bytes(0); # '0'
11 $size = format_bytes(2*1024); # '2.0K'
12
13 $size = format_bytes(1_234_890, bs => 1000); # '1.3M'
14 $size = format_bytes(1E9, bs => 1000); # '1.0G'
15
16 # the OO way
17 $human = Number::Bytes::Human->new(bs => 1000, si => 1);
18 $size = $human->format(1E7); # '10MB'
19 $human->set_options(zero => '-');
20 $size = $human->format(0); # '-'
21
23 THIS IS ALPHA SOFTWARE: THE DOCUMENTATION AND THE CODE WILL SUFFER
24 CHANGES SOME DAY (THANKS, GOD!).
25
26 This module provides a formatter which turns byte counts to usual
27 readable format, like '2.0K', '3.1G', '100B'. It was inspired in the
28 "-h" option of Unix utilities like "du", "df" and "ls" for "human-
29 readable" output.
30
31 From the FreeBSD man page of "df":
32 http://www.freebsd.org/cgi/man.cgi?query=df
33
34 "Human-readable" output. Use unit suffixes: Byte, Kilobyte,
35 Megabyte, Gigabyte, Terabyte and Petabyte in order to reduce the
36 number of digits to four or fewer using base 2 for sizes.
37
38 byte B
39 kilobyte K = 2**10 B = 1024 B
40 megabyte M = 2**20 B = 1024 * 1024 B
41 gigabyte G = 2**30 B = 1024 * 1024 * 1024 B
42 terabyte T = 2**40 B = 1024 * 1024 * 1024 * 1024 B
43
44 petabyte P = 2**50 B = 1024 * 1024 * 1024 * 1024 * 1024 B
45 exabyte E = 2**60 B = 1024 * 1024 * 1024 * 1024 * 1024 * 1024 B
46 zettabyte Z = 2**70 B = 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 B
47 yottabyte Y = 2**80 B = 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 B
48
49 I have found this link to be quite useful:
50
51 http://www.t1shopper.com/tools/calculate/
52
53 If you feel like a hard-drive manufacturer, you can start counting
54 bytes by powers of 1000 (instead of the generous 1024). Just use "bs
55 => 1000".
56
57 But if you are a floppy disk manufacturer and want to start counting in
58 units of 1024000 (for your "1.44 MB" disks)? Then use "bs =>
59 1_024_000".
60
61 If you feel like a purist academic, you can force the use of metric
62 prefixes according to the Dec 1998 standard by the IEC. Never mind the
63 units for base 1000 are "('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB',
64 'ZB', 'YB')" and, even worse, the ones for base 1024 are "('B', 'KiB',
65 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB')" with the horrible
66 names: bytes, kibibytes, mebibytes, etc. All you have to do is to use
67 "si => 1". Ain't that beautiful the SI system? Read about it:
68
69 http://physics.nist.gov/cuu/Units/binary.html
70
71 You can try a pure Perl "ls -lh"-inspired command with the one-liner,
72 er, two-liner:
73
74 $ perl -MNumber::Bytes::Human=format_bytes \
75 -e 'printf "%5s %s\n", format_bytes(-s), $_ for @ARGV' *
76
77 Why to write such a module? Because if people can write such things in
78 C, it can be written much easier in Perl and then reused, refactored,
79 abused. And then, when it is much improved, some brave soul can port it
80 back to C (if only for the warm feeling of painful programming).
81
82 OBJECTS
83 An alternative to the functional style of this module is the OO
84 fashion. This is useful for avoiding the unnecessary parsing of the
85 arguments over and over if you have to format lots of numbers
86
87 for (@sizes) {
88 my $fmt_size = format_bytes($_, @args);
89 ...
90 }
91
92 versus
93
94 my $human = Number::Format::Bytes->new(@args);
95 for (@sizes) {
96 my $fmt_size = $human->format($_);
97 ...
98 }
99
100 for TODO [TODO] MAKE IT JUST A MATTER OF STYLE: memoize _parse_args()
101 $seed == undef
102
103 FUNCTIONS
104 format_bytes
105 $h_size = format_bytes($size, @options);
106
107 Turns a byte count (like 1230) to a readable format like '1.3K'.
108 You have a bunch of options to play with. See the section "OPTIONS"
109 to know the details.
110
111 METHODS
112 new
113 $h = Number::Bytes::Human->new(@options);
114
115 The constructor. For details on the arguments, see the section
116 "OPTIONS".
117
118 format
119 $h_size = $h->format($size);
120
121 Turns a byte count (like 1230) to a readable format like '1.3K'.
122 The statements
123
124 $h = Number::Bytes::Human->new(@options);
125 $h_size = $h->format($size);
126
127 are equivalent to "$h_size = format_bytes($size, @options)", with
128 only one pass for the option arguments.
129
130 set_options
131 $h->set_options(@options);
132
133 To alter the options of a "Number::Bytes::Human" object. See
134 "OPTIONS".
135
136 OPTIONS
137 BASE
138 block | base | block_size | bs => 1000 | 1024 | 1024000
139 base_1024 | block_1024 | 1024 => 1
140 base_1000 | block_1000 | 1000 => 1
141
142 The base to be used: 1024 (default), 1000 or 1024000.
143
144 Any other value throws an exception.
145
146 SUFFIXES
147 suffixes => 1000 | 1024 | 1024000 | si_1000 | si_1024 | $arrayref
148
149 By default, the used suffixes stand for '', 'K', 'M', ... for base
150 1024 and '', 'k', 'M', ... for base 1000 (which are indeed the
151 usual metric prefixes with implied unit as bytes, 'B'). For the
152 weird 1024000 base, suffixes are '', 'M', 'T', etc.
153
154 ZERO
155 zero => string | undef
156
157 The string 0 maps to ('0' by default). If "undef", the general case
158 is used. The string may contain '%S' in which case the suffix for
159 byte is used.
160
161 format_bytes(0, zero => '-') => '-'
162
163 METRIC SYSTEM
164 si => 1
165
166 ROUND
167 round_function => $coderef
168 round_style => 'ceil' | 'floor'
169
170 TO_S
171 QUIET
172 quiet => 1
173
174 Suppresses the warnings emitted. Currently, the only case is when
175 the number is large than "$base**(@suffixes+1)".
176
177 EXPORT
178 It is alright to import "format_bytes", but nothing is exported by
179 default.
180
182 "unknown round style '$style'";
183
184 "invalid base: $block (should be 1024, 1000 or 1024000)";
185
186 "round function ($args{round_function}) should be a code ref";
187
188 "suffixes ($args{suffixes}) should be 1000, 1024, 1024000 or an array ref";
189
190 "negative numbers are not allowed" (??)
191
193 A function "parse_bytes"
194
195 parse_bytes($str, $options)
196
197 which transforms '1k' to 1000, '1K' to 1024, '1MB' to 1E6, '1M' to
198 1024*1024, etc. (like gnu du).
199
200 $str =~ /^\s*(\d*\.?\d*)\s*(\S+)/ # $num $suffix
201
203 lib/human.c and lib/human.h in GNU coreutils.
204
205 The "_convert()" solution by COG in Filesys::DiskUsage.
206
208 Please report bugs via CPAN RT
209 http://rt.cpan.org/NoAuth/Bugs.html?Dist=Number-Bytes-Human
210 <http://rt.cpan.org/NoAuth/Bugs.html?Dist=Number-Bytes-Human> or
211 mailto://bug-Number-Bytes-Human@rt.cpan.org <mailto://bug-Number-Bytes-
212 Human@rt.cpan.org>. I will not be able to close the bug as
213 BestPractical ignore my claims that I cannot log in, but I will answer
214 anyway.
215
217 Adriano R. Ferreira, <ferreira@cpan.org>
218
220 Copyright (C) 2005-2007 by Adriano R. Ferreira
221
222 This library is free software; you can redistribute it and/or modify it
223 under the same terms as Perl itself.
224
225
226
227perl v5.12.0 2007-04-05 Human(3)