[deutsch]

Formatting decimal numbers

Kontrast provides functions to format JavaScript floating point numbers as decimal numbers.

The browser natively offers this capability using the toString, toFixed, toExponential and toPrecision methods of the Number object. These functions work for many applications, but you might experience some edge cases when trying to write a programmatic number formatter using these functions.

Alternatively, the browser provides the Intl.NumberFormat API which focuses mainly on internationalization.

The Kontrast API provides a way to inspect a decimal number by obtaining its least significant and its most significant decimal exponent using the function kontrast.inspectDecimal. Additionally, the method kontrast.formatDecimal allows for

  • formatting the output with a customizable exponent
  • while rounding to a custom decimal exponent
  • and providing a separate output for the integer and fractional part of the number for custom formatting, e.g. with KaTeX.

Inspecting a number

Input

The function is called as kontrast.inspectDecimal(options), where options is an object with the following properties:

  • value: finiteNonZeroNumber
    The input value to be inspected as a normal JavaScript number (i.e. a IEEE 754 binary 64 bit floating point number).

Output

The function returns an object with the following properties:

  • mostSignificantExponent: number
    The most significant decimal exponent of the normalized floating point input value. This effectively is also the (decimal) order of magnitude.
  • leastSignificantExponent: number
    The least significant decimal exponent of the normalized floating point input value. The least significant exponent is less than or equal to the most significant exponent. The maximum difference between the most significant exponent and the least significant exponent is 17.

Formatting a decimal number

Input

The function is called as kontrast.formatDecimal(options), where options is an object with the following properties:

  • value: finiteNumber
    The input value to be inspected as a normal JavaScript number (i.e. a IEEE 754 binary 64 bit floating point number).
  • exponent: integer
    The decimal exponent of the formatted result
  • leastSignificantExponent: integer
    The least significant exponent of the formatted result

Output

The function returns an object with the following properties:

  • sign: number
    A number which is either -1 or 1 corresponding to the sign of the input value. Notice that this sign also works for the signed zero (i.e. the sign of the input value +0 is +1 and the sign of the input value -0 is -1).
  • isImprecise: boolean
    This boolean indicates whether the formatted number is an imprecise (i.e. rounded) representation of the original input value.
  • integer: string
    The integer part of the result.
  • fractional: string
    The fractional part of the result. If there is no fractional part, this property is an empty string.
  • value: number
    The formatted result, represented as a JavaScript number.
  • significant: number
    The integer and the fractional part, but not the exponent, represented as a JavaScript number.