cuducos/elm-format-number/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
, 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:

  • Western separates digits by thousands (e.g.: 1000000 might be separated as 1,000,000).
  • Indian separates digits by the thousand and, from there, by hundreds (e.g.: 1000000 might be separated as 10,00,000).
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:

  • the number needs to be negative to predict the negative suffix and prefix

  • the number needs to have decimals to predict the decimal separator

  • the number needs use different separators for thousands and for decimals to predict these separators

  • the number's module needs to be greater than (or equal to) 100.000 and lesser than (or equal to) 999.999 to predict the right numeric system, i.e. Western or Indian

    fromString "3.1415" -> Nothing

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

    fromString "-3,14,159.265" --> Just { base --> | decimals = Exact 3 --> , thousandSeparator = "," --> , decimalSeparator = "." --> , negativePrefix = "-" --> , system = Indian --> }

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.