decimal-arithmetic- An implementation of the General Decimal Arithmetic Specification

Safe HaskellTrustworthy




The functions in this module implement conversions between Decimal and String as described in the General Decimal Arithmetic Specification.

Because these functions are also used to implement Show and Read class methods, it is not usually necessary to import this module except to use the toEngineeringString function.


Numeric string syntax

(The following description is from the General Decimal Arithmetic Specification.)

Strings which are acceptable for conversion to the abstract representation of numbers, or which might result from conversion from the abstract representation to a string, are called numeric strings.

A numeric string is a character string that describes either a finite number or a special value.

  • If it describes a finite number, it includes one or more decimal digits, with an optional decimal point. The decimal point may be embedded in the digits, or may be prefixed or suffixed to them. The group of digits (and optional point) thus constructed may have an optional sign (“+” or “-”) which must come before any digits or decimal point.

    The string thus described may optionally be followed by an “E” (indicating an exponential part), an optional sign, and an integer following the sign that represents a power of ten that is to be applied. The “E” may be in uppercase or lowercase.

  • If it describes a special value, it is one of the case-independent names “Infinity”, “Inf”, “NaN”, or “sNaN” (where the first two represent infinity and the second two represent quiet NaN and signaling NaN respectively). The name may be preceded by an optional sign, as for finite numbers. If a NaN, the name may also be followed by one or more digits, which encode any diagnostic information.

No blanks or other white space characters are permitted in a numeric string.


Some numeric strings are:

    "0"          -- zero
    "12"         -- a whole number
   "-76"         -- a signed whole number
    "12.70"      -- some decimal places
    "+0.003"     -- a plus sign is allowed, too
   "017."        -- the same as 17
      ".5"       -- the same as 0.5
    "4E+9"       -- exponential notation
     "0.73e-7"   -- exponential notation, negative power
    "Inf"        -- the same as Infinity
    "-infinity"  -- the same as -Inf
    "NaN"        -- not-a-Number
    "NaN8275"    -- diagnostic NaN


  1. A single period alone or with a sign is not a valid numeric string.
  2. A sign alone is not a valid numeric string.
  3. Significant (after the decimal point) and insignificant leading zeros are permitted.

Conversion to numeric string

toScientificString :: Decimal p r -> ShowS Source #

Convert a number to a string, using scientific notation if an exponent is needed.

toEngineeringString :: Decimal p r -> ShowS Source #

Convert a number to a string, using engineering notation if an exponent is needed.

Conversion from numeric string

toNumber :: ReadP (Decimal PInfinite r) Source #

Convert a string to a number, as defined by its abstract representation. The string is expected to conform to the numeric string syntax described here.