Cldr v1.5.1 Cldr.Digits View Source

Abstract representation of number (integer, float, Decimal) in tuple form and functions for transformations on number parts.

Representing a number as a list of its digits, and integer representing where the decimal point is placed and an integer representing the sign of the number allow more efficient transforms on the various parts of the number as happens during the formatting of a number for string output.

Link to this section Summary

Types

t()

Defines a number in a tuple form of three parts

Functions

Returns the fractional part of an integer, float or Decimal as an integer

Returns the number of decimal digits in a number (integer, float, Decimal)

Returns the number of decimal digits in the integer part of a number

Returns the number of leading zeros in a Decimal fraction

Remove trailing zeroes from the integer part of a number and returns the integer part without trailing zeros

Computes a iodata list of the digits of the given IEEE 754 floating point number, together with the location of the decimal point as {digits, place, positive} A “compact” representation is returned, so there may be fewer digits returned than the decimal point location

Takes a list of digits and coverts them back to a number of the same type as number

Converts given number to a list representation

Link to this section Types

Link to this type t() View Source
t() :: {[0..9, ...], non_neg_integer(), 1 | -1}

Defines a number in a tuple form of three parts:

  • A list of digits (0..9) representing the number

  • A digit representing the place of the decimal points in the number

  • a 1 or -1 representing the sign of the number

A number in integer, float or Decimal forma can be converted to digit form with Digits.to_digits/1

THe digits can be converted back to normal form with Cldr.Digits.to_integer/1, Cldr.Digits.to_float/1 and Cldr.Digits.to_decimal/1.

Link to this section Functions

Link to this function fixup(r, s, m_plus, m_minus, k, low_ok, high_ok, float) View Source
Link to this function fraction_as_integer(number) View Source
fraction_as_integer(Cldr.Math.number_or_decimal() | {list(), list(), 1 | -1}) ::
  integer()

Returns the fractional part of an integer, float or Decimal as an integer.

  • number can be either a float, Decimal or integer although an integer has no fraction part and will therefore always return 0.

Examples

iex> Cldr.Digits.fraction_as_integer(123.456)
456

iex> Cldr.Digits.fraction_as_integer(Decimal.new("123.456"))
456

iex> Cldr.Digits.fraction_as_integer(1999)
0
Link to this function fraction_as_integer(number, rounding) View Source
Link to this function number_of_digits(number) View Source
number_of_digits(
  Cldr.Math.number_or_decimal()
  | list()
  | {[integer(), ...], integer() | [integer(), ...], -1 | 1}
) :: integer()

Returns the number of decimal digits in a number (integer, float, Decimal)

Options

  • number is an integer, float or Decimal or a list (which is assumed to contain digits).

Examples

iex> Cldr.Digits.number_of_digits(1234)
4

iex> Cldr.Digits.number_of_digits(Decimal.new("123456789"))
9

iex> Cldr.Digits.number_of_digits(1234.456)
7

iex> Cldr.Digits.number_of_digits(1234.56789098765)
15

iex> Cldr.Digits.number_of_digits '12345'
5
Link to this function number_of_integer_digits(number) View Source
number_of_integer_digits(
  Cldr.Math.number_or_decimal()
  | list()
  | {[integer(), ...], integer() | [integer(), ...], -1 | 1}
) :: integer()

Returns the number of decimal digits in the integer part of a number.

Options

  • number is an integer, float or Decimal or a list (which is assumed to contain digits).

Examples

iex> Cldr.Digits.number_of_integer_digits(1234)
4

iex> Cldr.Digits.number_of_integer_digits(Decimal.new("123456789"))
9

iex> Cldr.Digits.number_of_integer_digits(1234.456)
4

iex> Cldr.Digits.number_of_integer_digits '12345'
5
Link to this function number_of_leading_zeros(number) View Source
number_of_leading_zeros(Cldr.Math.number_or_decimal() | [integer(), ...]) ::
  integer()

Returns the number of leading zeros in a Decimal fraction.

  • number is an integer, float or Decimal

Returns the number of leading zeros in the fractional part of a number.

Examples

iex> Cldr.Digits.number_of_leading_zeros(Decimal.new(0.0001))
3
Link to this function remove_trailing_zeros(number) View Source
remove_trailing_zeros(Cldr.Math.number_or_decimal() | [integer(), ...]) ::
  integer() | [integer(), ...]

Remove trailing zeroes from the integer part of a number and returns the integer part without trailing zeros.

  • number is an integer, float or Decimal.

Examples

iex> Cldr.Digits.remove_trailing_zeros(1234000)
1234
Link to this function scale(r, s, m_plus, m_minus, low_ok, high_ok, float) View Source

Computes a iodata list of the digits of the given IEEE 754 floating point number, together with the location of the decimal point as {digits, place, positive} A “compact” representation is returned, so there may be fewer digits returned than the decimal point location

Link to this function to_number(digits, number) View Source

Takes a list of digits and coverts them back to a number of the same type as number

Link to this function to_tuple(number) View Source
to_tuple(Decimal.t() | number()) :: {list(), list(), integer()}

Converts given number to a list representation.

Given an IEEE 754 float, computes the shortest, correctly rounded list of digits that converts back to the same Double value when read back with String.to_float/1. Implements the algorithm from “Printing Floating-Point Numbers Quickly and Accurately” in Proceedings of the SIGPLAN ‘96 Conference on Programming Language Design and Implementation.

Returns a tuple comprising a charlist for the integer part, a charlist for the fractional part and an integer for the sign