cuducos / elm-format-number / FormatNumber.Locales

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


type Decimals
    = Min Basics.Int
    | Max Basics.Int
    | Exact Basics.Int

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


type alias Locale =
{ decimals : Decimals
, system : System
, thousandSeparator : String
, decimalSeparator : String
, negativePrefix : String
, negativeSuffix : String
, positivePrefix : String
, positiveSuffix : String
, zeroPrefix : String
, zeroSuffix : String 
}

This is the Locale type and constructor.


type System
    = Western
    | Indian

The System type contains different numeric systems currently supported:

base : Locale

The base locale is a Western numeric system which 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 -> Maybe Locale

Reads a JavaScript's Number.toLocaleString() return value to try to create a 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 * -1000000).toLocaleString()
  }
}\)

Then we use fromString to read this value from the flag.

The input value should be a number that offers enough hints so fromString can make a informed prediction of the Locale:

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.

indianLocale : Locale

Locale used in India. It is similar to usLocale, but uses the Indian numeric system.

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.