Learn R Programming

gdata (version 2.7.1)

humanReadable: Print byte size in human readable format

Description

humanReadable converts byte size in human readable format such as kB, MB, GB, etc.

Usage

humanReadable(x, standard="SI", digits=1, width=3, sep=" ")

Arguments

x
integer, byte size
standard
character, "SI" for powers of 1000 or anything else for powers of 1024, see details
digits
integer, number of digits after decimal point
width
integer, width of number string
sep
character, separator between number and unit

Value

  • Byte size in human readable format as character with proper unit symbols added at the end of the string.

Details

Basic unit used to store information in computers is a bit. Bits are represented as zeroes and ones - binary number system. Although, the binary number system is not the same as the decimal number system, decimal prefixes for binary multiples such as kilo and mega are often used. In the decimal system kilo represent 1000, which is close to $1024 = 2^{10}$ in the binary system. This sometimes causes problems as it is not clear which powers (2 or 10) are used in a notation like 1 kB. To overcome this problem International Electrotechnical Commission (IEC) has provided the following solution to this problem:

lrcll{ Name System Symbol Size Conversion byte binary B $2^3$ 8 bits kilobyte decimal kB $10^3$ 1000 bytes kibibyte binary KiB $2^{10}$ 1024 bytes megabyte decimal MB $(10^3)^2$ 1000 kilobytes mebibyte binary MiB $(2^{10})^2$ 1024 kibibytes gigabyte decimal GB $(10^3)^3$ 1000 megabytes gibibyte binary GiB $(2^{10})^3$ 1024 mebibytes terabyte decimal TB $(10^3)^4$ 1000 gigabytes tebibyte binary TiB $(2^{10})^4$ 1024 gibibytes petabyte decimal PB $(10^3)^5$ 1000 terabytes pebibyte binary PiB $(2^{10})^5$ 1024 tebibytes exabyte decimal EB $(10^3)^6$ 1000 petabytes exbibyte binary EiB $(2^{10})^6$ 1024 pebibytes zettabyte decimal ZB $(10^3)^7$ 1000 exabytes zebibyte binary ZiB $(2^{10})^7$ 1024 exbibytes yottabyte decimal YB $(10^3)^8$ 1000 zettabytes yebibyte binary YiB $(2^{10})^8$ 1024 zebibytes }

where Zi and Yi are GNU extensions to IEC. To get the output in the decimal system (powers of 1000) use standard="SI". Otherwise IEC standard (powers of 1024) is used.

For printout both digits and width can be specified. If width is NULL, all values have given number of digits. If width is not NULL, output is rounded to a given width and formated similar to human readable format of ls, df or du shell commands.

References

Wikipedia: http://en.wikipedia.org/wiki/Byte http://en.wikipedia.org/wiki/SI_prefix http://en.wikipedia.org/wiki/Binary_prefix

GNU manual for coreutils: http://www.gnu.org/software/coreutils/manual/html_node/Block-size.html#Block-size

See Also

object.size, ll

Examples

Run this code
baseSI <- 10
powerSI <- seq(from=3, to=27, by=3)
SI0 <- (baseSI)^powerSI
k <- length(SI0) - 1
SI1 <- SI0 - SI0 / c(2, runif(n=k, min=1.01, max=5.99))
SI2 <- SI0 + SI0 / c(2, runif(n=k, min=1.01, max=5.99))

baseIEC <- 2
powerIEC <- seq(from=10, to=90, by=10)
IEC0 <- (baseIEC)^powerIEC
IEC1 <- IEC0 - IEC0 / c(2, runif(n=k, min=1.01, max=5.99))
IEC2 <- IEC0 + IEC0 / c(2, runif(n=k, min=1.01, max=5.99))

cbind(humanReadable(x=SI1, width=NULL, digits=3),
      humanReadable(x=SI0, width=NULL, digits=2),
      humanReadable(x=SI2, width=NULL, digits=1),
      humanReadable(x=IEC1, standard="IEC", width=7, digits=3),
      humanReadable(x=IEC0, standard="IEC", width=7, digits=2),
      humanReadable(x=IEC2, standard="IEC", width=7, digits=1))

Run the code above in your browser using DataLab