basti1302 / elm-human-readable-filesize / 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:

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

Supported binary/IEC units:

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 : Basics.Int -> String

Formats the given file size with the default settings.

Convenience function for

let
    ( size, unit ) =
        formatWithSplit settings num
in
size ++ " " ++ unit

formatSplit : Basics.Int -> ( String, String )

Formats the given file size with the default settings, returning the number and units separately, in a tuple.

formatBase2 : Basics.Int -> String

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

formatBase2Split : Basics.Int -> ( String, String )

Formats the given file size with the binary/base2/IEC unit, returning the number and units separately, in a tuple.

formatWith : Settings -> Basics.Int -> String

Formats the given file size with the given settings.

formatWithSplit : Settings -> Basics.Int -> ( String, String )

Formats the given file size with the given settings, returning the number and units separately, in a tuple.

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 : Basics.Int
, decimalSeparator : String 
}

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


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.