Cldr v0.1.0 Cldr.Digits

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.

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 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

Types

t()
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.

Functions

fixup(r, s, m_plus, m_minus, k, low_ok, high_ok, float)
fraction_as_integer(number)
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.Math.fraction_as_integer(123.456)
456

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

iex> Cldr.Math.fraction_as_integer(1999)
0
number_of_integer_digits(number)
number_of_integer_digits(Cldr.Math.number_or_decimal) :: integer

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

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

Examples

iex> Cldr.Math.number_of_integer_digits(1234)
4

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

iex> Cldr.Math.number_of_integer_digits(1234.456)
4

iex> Cldr.Digits.number_of_integer_digits '12345'
5
number_of_leading_zeros(number)
number_of_leading_zeros(%Decimal{coef: term, exp: term, sign: term}) :: 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.Math.number_of_leading_zeros(Decimal.new(0.0001))
3
remove_trailing_zeros(number)
remove_trailing_zeros(Cldr.Math.number_or_decimal) :: 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.Math.remove_trailing_zeros(1234000)
1234
scale(r, s, m_plus, m_minus, low_ok, high_ok, float)
to_decimal(arg)
to_digits(float)

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

to_float(arg)
to_integer(arg)
to_number(digits, number)

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

to_tuple(number)
to_tuple(Decimal.t | number) :: {List.t, List.t, 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