cuducos/elm-format-number/8.1.4

NOTE: the latest version is 9.0.1

FormatNumber.Locales

These locales and its names are based on this International Language Environments Guide

= Min Int
| Max Int
| Exact Int

The Decimals type contains different strategies for handling the number of decimals when formatting the number:

  • Min Int shows at least a certain amount of decimal digits, adding trailing zeros if needed.
  • Max Int shows up to a certain amount of decimal digits, discarding trailing zeros if needed.
  • Exact Int shows an exact number of decimal digits, adding trailing zeros if needed.
type alias Locale =
{ decimals : Decimals
, thousandSeparator : String
, decimalSeparator : String
, negativePrefix : String
, negativeSuffix : String
, positivePrefix : String
, positiveSuffix : String
, zeroPrefix : String
, zeroSuffix : String
}

This is the Locale type and constructor.

base : Locale

The base locale uses unicode minus (U+2212) instead of a hyphen/dash for visual consistency.

Note that String.toFloat does not understand unicode minus (U+2212), thus it will return Nothing for negative number strings formatted using base locale.

If you need a result compatible with String.toFloat, consider creating your own locale with hypen as the negativePrefix or use a custom string to float function that handles U+2212 if need be.

fromString : String -> Locale

Reads a JavaScript's Number.toLocaleString() return value to create the Locale according to the user's local settings. This is useful when combined with Elm's Flags, e.g.:

Elm.Main.init\({
  flags: {
    numberFormat: (Math.PI * -1000).toLocaleString()
  }
}\)

Then we use fromString to read this value from the flag (although it does not cover the case with zero decimal places). If it fails to parse the string to a Locale, it will return the base locale.

fromString "-3,141.593"
--> { base
--> | decimals = Exact 3
--> , thousandSeparator = ","
--> , decimalSeparator = "."
--> , negativePrefix = "-"
--> }

Pre-defined locales

frenchLocale : Locale

Locale used in France, Canada, Finland and Sweden. It has 3 decimals digits, uses a thin space (U+202F) as thousand separator and a , as decimal separator. It uses a minus sign (not a hyphen) as a prefix for negative numbers, but no suffix or prefix for positive numbers.

spanishLocale : Locale

Locale used in Spain, Italy and Norway. It has 3 decimals digits, uses a . as thousand separator and a , as decimal separator. It uses a minus sign (not a hyphen) as a prefix for negative numbers, but no suffix or prefix for positive numbers.

usLocale : Locale

Locale used in the United States, Great Britain and Thailand. It has 2 decimals digits, uses a , as thousand separator and a . as decimal separator. It uses a minus sign (not a hyphen) as a prefix for negative numbers, no suffix or prefix for positive numbers.