Skip to content
Format numbers as pretty strings
Elm
Branch: master
Clone or download
Pull request Compare This branch is 4 commits ahead, 5 commits behind cuducos:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci
src
tests
.gitignore
CHANGELOG
LICENSE
README.md
elm.json
package-lock.json
package.json

README.md

Elm Format Number Build Status

This simple Elm package formats Float numbers as pretty strings.

Format

The format function formats Float numbers using a locale with settings:

import FormatNumber exposing (format)
import FormatNumber.Locales exposing (spanishLocale, usLocale)

format usLocale (pi * 1000)  --> "3,141.59"
format spanishLocale (pi * 1000)  --> "3.141,59"

It is flexible enough to deal with different number of decimals, different thousand separators, different decimal separator, and different ways to represent negative numbers — all that is possible using Locales.

import FormatNumber exposing (format)
import FormatNumber.Locales exposing (Locale, usLocale)

sharesLocale : Locale
sharesLocale =
    { usLocale
        | decimals = 3
        , negativePrefix = "("
        , negativeSuffix = ")"
    }

format usLocale -pi --> "−3.14"
format sharesLocale -pi --> "(3.142)"

Humanize

The humanize function limits the number of decimals according to the Locale but may remove zeros from the tail in order to make it more humam readable. The RemoveZero removes any tail 0 from the result, while KeepZeros only remove decimals if all decimal digits are 0:

import FormatNumber exposing (humanize)
import FormatNumber.Humanize exposing (ZeroStrategy(..))
import FormatNumber.Locales exposing (usLocale)

humanize usLocale RemoveZeros 10.00 --> "10"
humanize usLocale RemoveZeros 10.10 --> "10.1"

humanize usLocale KeepZeros 10.00 --> "10"
humanize usLocale KeepZeros 10.10 --> "10.10"

Docs

The API is further documented in package.elm-lang.org.

Tests

This package uses elm-verify-examples, all the examples in the documentation are automatically tested:

$ npm install
$ npm test
You can’t perform that action at this time.