bytes

This module contains functions for formatting amounts of data to Strings (e.g. "100.0B", "1.5GB").

Usage generally looks like this:

bytes.Kilobytes(2000.0) |> bytes.humanise |> bytes.to_string // "2.0MB"

// or, if you don't want to change the unit
bytes.Kilobytes(2000.0) |> bytes.to_string // "2000.0KB"

Note: This module currently uses multiples of 1000 (kilobytes, megabytes etc), NOT multiples of 1024 (kibibytes, mebibytes, etc). Support for 1024-multiples will likely be added later!

Types

The main type for holding data amount information.

Use its constructors directly to specify a unit for the value you want to format.

pub type Bytes {
  Bytes(Float)
  Kilobytes(Float)
  Megabytes(Float)
  Gigabytes(Float)
  Terabytes(Float)
}

Constructors

  • Bytes(Float)
  • Kilobytes(Float)
  • Megabytes(Float)
  • Gigabytes(Float)
  • Terabytes(Float)

Functions

pub fn humanise(this bytes: Bytes) -> Bytes

Convert a value to a more optimal unit, if possible.

Example:

bytes.Megabytes(0.5) |> bytes.humanise // bytes.Kilobytes(500.0)
pub fn to_string(this bytes: Bytes) -> String

Format a value as a String, rounded to at most 2 decimal places, followed by a unit suffix.

Example:

bytes.Gigabytes(30.125) |> bytes.to_string // "30.13GB"
Search Document