basti1302/elm-human-readable-filesize/1.1.0

NOTE: this package is not compatible with Elm 0.19.1

Filesize

This library converts a file size in bytes into a human readable string.

Examples:

format 1234 == "1.23 kB"
format 238674052 == "238.67 MB"
format 543 == "543 B"

You can either use decimal units (also known as base 10 units, these are the default) or binary (also called base 2 or IEC units).

Supported decimal units:

  • 1 byte (B)
  • 1 kilobyte (kB) = 1000 bytes
  • 1 megabyte (MB) = 1000 kilobytes
  • 1 gigabyte (GB) = 1000 megabytes
  • 1 terabyte (TB) = 1000 gigabytes
  • 1 petabyte (PB) = 1000 terabytes
  • 1 exabyte (EB) = 1000 petabyte

Larger decimal units (zettabyte (ZB), yottabyte (YB), ...) are not supported.

Supported binary/IEC units:

  • 1 byte (B)
  • 1 kibibyte (KiB) = 1024 bytes
  • 1 mebibyte (MiB) = 1024 kibibytes
  • 1 gibibyte (GiB) = 1024 mebibytes
  • 1 tebibyte (TiB) = 1024 gibibytes
  • 1 pebibyte (PiB) = 1024 tebibyte

Larger binary units (exbibyte (EiB), zebibyte (ZiB), yobibytej (YiB), ...)) are not supported.

For decimal/base 10 units, the number of bytes is divided by 10^3 when going to the next larger unit. For binary/base 2 units, the number of bytes is divided by 2^10 (1024) each time. (For binary units also see https://en.wikipedia.org/wiki/Kibibyte.)

Usage

format : Int -> String

Formats the given file size with the default settings.

formatBase2 : Int -> String

Formats the given file size with the binary/base2/IEC unit.

formatWith : Settings -> Int -> String

Formats the given file size with the given settings.

defaultSettings : Settings

The default settings. When using formatWith, it is recommended to obtain a settings record with this function and modify the settings to your liking.

type alias Settings =
{ units : Units
, decimalPlaces : Int
, decimalSeparator : String
}

Use a settings record together with formatWith to customize the formatting process. The available options are:

  • units: use either decimal or binary/IEC units (the default is to use decimal units),
  • decimalPlaces: the number of decimal places (digits after the decimal separator, the default is 2) and
  • decimalSeparator: the decimal separator to use (default ".").
type Units
= Base10
| Base2

The two possible unit types, either decimal/base 10 (kb, MB, GB, ...) or binary/IEC/base 2 (KiB, MiB, GiB, ...), see above.