finanza/decimal
Fixed-point decimal numbers with explicit rounding.
A Decimal is a coefficient and a base-10 exponent, so 12.30 is
the coefficient 1230 with the exponent -2. Operations that can
lose digits take a RoundingMode.
The coefficient, and the integer a positive exponent stands for,
stay within ±9,007,199,254,740,991 (2^53 − 1) on both targets, so
results are the same on Erlang and JavaScript. Results outside
that range are PrecisionExceeded errors.
Types
Why an arithmetic operation failed.
pub type ArithmeticError {
DivisionByZero
PrecisionExceeded
}
Constructors
-
DivisionByZeroThe divisor was zero.
-
PrecisionExceededThe result does not fit in the supported range.
Why parse rejected its input.
pub type ParseError {
EmptyInput
InvalidCharacter(character: String, position: Int)
MultipleDecimalPoints
MultipleSigns
NoDigits
ParsedValueTooLarge
}
Constructors
-
EmptyInputThe input was empty or only whitespace.
-
InvalidCharacter(character: String, position: Int)The input has a character that is not a digit, a sign, a
.or an exponent marker.positioncounts graphemes from 0 after the surrounding whitespace is trimmed. -
MultipleDecimalPointsThe input has more than one
.. -
MultipleSignsThe input has more than one sign, or a sign after the first character.
-
NoDigitsThe input has no digits, such as
"+"or".". -
ParsedValueTooLargeThe value is outside the supported range.
How to round when an operation drops digits.
HalfEven (banker’s rounding) is the usual choice for money.
pub type RoundingMode {
HalfEven
HalfUp
HalfDown
Up
Down
Ceiling
Floor
}
Constructors
-
HalfEvenRound half-way values to the even neighbour: 2.5 to 2, 3.5 to 4.
-
HalfUpRound half-way values away from zero: 2.5 to 3, -2.5 to -3.
-
HalfDownRound half-way values toward zero: 2.5 to 2, -2.5 to -2.
-
UpRound away from zero: 2.1 to 3, -2.1 to -3.
-
DownRound toward zero: 2.9 to 2, -2.9 to -2.
-
CeilingRound toward positive infinity: 2.1 to 3, -2.9 to -2.
-
FloorRound toward negative infinity: 2.9 to 2, -2.1 to -3.
Values
pub fn add(
a: Decimal,
b: Decimal,
) -> Result(Decimal, ArithmeticError)
Adds two decimals. The result has the smaller of the two exponents, except that adding zero returns the other decimal unchanged.
Returns Error(PrecisionExceeded) when the sum is out of range.
pub fn coefficient(decimal: Decimal) -> Int
Returns the coefficient, so 12.30 gives 1230.
pub fn compare(a: Decimal, b: Decimal) -> order.Order
Compares two decimals by value, so 1.0 and 1.00 are equal.
pub fn divide(
a: Decimal,
b: Decimal,
digits digits: Int,
rounding mode: RoundingMode,
) -> Result(Decimal, ArithmeticError)
Divides a by b and rounds the quotient to digits places after
the decimal point.
Returns Error(DivisionByZero) when b is zero, and
Error(PrecisionExceeded) when the quotient at that many places is
out of range, which happens from about 15 places for 1 / 3.
Examples
let assert Ok(ten) = decimal.from_int(10)
let assert Ok(three) = decimal.from_int(3)
decimal.divide(ten, three, digits: 2, rounding: decimal.HalfEven)
|> result.map(decimal.to_string)
// -> Ok("3.33")
pub fn equal(a: Decimal, b: Decimal) -> Bool
Returns True when two decimals have the same value. == compares
the representation instead, so 1.0 == 1.00 is False but
equal(1.0, 1.00) is True.
pub fn exponent(decimal: Decimal) -> Int
Returns the base-10 exponent, so 12.30 gives -2.
pub fn format(
decimal: Decimal,
thousands thousands: String,
decimal_separator decimal_separator: String,
) -> String
Renders the decimal like to_string, with thousands between
groups of three integer digits and decimal_separator in place of
the ..
Examples
let assert Ok(price) = decimal.parse("1234567.5")
decimal.format(price, thousands: ".", decimal_separator: ",")
// -> "1.234.567,5"
pub fn from_float(value: Float) -> Result(Decimal, ParseError)
Creates a decimal from the text float.to_string gives for a float.
0.5 becomes 0.5, and 0.1 +. 0.2 becomes
0.30000000000000004. Prefer parse or new when the value is
not already a float.
pub fn from_int(value: Int) -> Result(Decimal, Nil)
Creates a decimal from an integer.
Returns Error(Nil) when the integer is outside
±9,007,199,254,740,991.
pub fn is_negative(decimal: Decimal) -> Bool
Returns True when the decimal is less than zero.
pub fn is_positive(decimal: Decimal) -> Bool
Returns True when the decimal is greater than zero.
pub fn multiply(
a: Decimal,
b: Decimal,
) -> Result(Decimal, ArithmeticError)
Multiplies two decimals without rounding. The exponents add up, so
1.5 × 0.25 is 0.375.
Returns Error(PrecisionExceeded) when the product is out of range.
pub fn new(
coefficient coefficient: Int,
exponent exponent: Int,
) -> Result(Decimal, Nil)
Creates the decimal coefficient × 10^exponent.
Returns Error(Nil) when the value is outside the supported range.
Examples
decimal.new(coefficient: 1234, exponent: -2) |> result.map(decimal.to_string)
// -> Ok("12.34")
decimal.new(coefficient: 1, exponent: 16)
// -> Error(Nil)
pub fn parse(input: String) -> Result(Decimal, ParseError)
Parses a decimal such as "12.30", "-0.5", "+7" or "1.5e-3".
Leading and trailing Unicode whitespace is ignored, including
no-break spaces. The exponent is kept, so "12.30" and "12.3"
parse to different representations of the same number.
Examples
decimal.parse(" 12.30 ") |> result.map(decimal.to_string)
// -> Ok("12.30")
decimal.parse("1.2.3")
// -> Error(decimal.MultipleDecimalPoints)
pub fn round(
decimal: Decimal,
digits digits: Int,
rounding mode: RoundingMode,
) -> Result(Decimal, ArithmeticError)
Rounds the decimal to digits places after the decimal point.
The result always has exactly digits places: 12.3 rounded to 2
places is 12.30. A negative digits rounds to tens, hundreds and
so on. Returns Error(PrecisionExceeded) when the padded or rounded
value is out of range.
Examples
let assert Ok(price) = decimal.parse("2.345")
decimal.round(price, digits: 2, rounding: decimal.HalfEven)
|> result.map(decimal.to_string)
// -> Ok("2.34")
let assert Ok(price) = decimal.parse("2.345")
decimal.round(price, digits: 2, rounding: decimal.HalfUp)
|> result.map(decimal.to_string)
// -> Ok("2.35")
pub fn subtract(
a: Decimal,
b: Decimal,
) -> Result(Decimal, ArithmeticError)
Subtracts b from a. The result has the smaller of the two
exponents.
pub fn to_int(decimal: Decimal) -> Result(Int, Nil)
Converts a whole-number decimal to an Int.
Returns Error(Nil) when the decimal has a non-zero fractional
part. Use round with digits: 0 first to drop it.
Examples
decimal.parse("12.00") |> result.try(decimal.to_int)
// -> Ok(12)
decimal.parse("12.34") |> result.try(decimal.to_int)
// -> Error(Nil)
pub fn to_string(decimal: Decimal) -> String
Renders the decimal with every digit of its exponent, so
new(coefficient: 100, exponent: -2) is "1.00" and
new(coefficient: 12, exponent: 2) is "1200".