Module Relude_Decimal
Relude.Decimal
contains a type t
which represents arbitrary precision numeric values, backed by an int
mantissa and an int
exponent. This can be useful for representing currency values without loss in precision or floating point errors.
Because the mantissa and exponent are backed by ints, there is a fairly restrictive range of values that can be represented.
let a = Decimal(12345, -2); // 123.45
let b = Decimal(6789, 3); // 6789000
type t
=
;|
Decimal(mantissa, exponent)
Represents an arbitrary precision number, backed by an integer mantissa, and integer exponent.
type rounding
=
;|
Truncate
|
RoundUp
|
RoundDown
|
RoundUpOrDown
Represents a preference for rounding a
Decimal
value, when applicable.
let make: int => int => t;
Decimal.make
constructs aDecimal
from amantissa
andexponent
.
let fromInt: int => t;
Decimal.fromInt
constructs aDecimal
from anint
.
let show: t => string;
Decimal.fromString
attempts to parse aDecimal
from astring
Decimal.show
renders theDecimal
value to astring
, as if theDecimal
was represented as afloat
.Decimal(12345, -2) |> Decimal.toString // "123.45" Decimal(12345, 3) |> Decimal.toString // "12345000"
let tenToThePowerOfPositive: int => int;
Decimal.round
rounds aDecimal
using the given rounding strategy.Decimal.tenToThePowerOfPositive
computes the value of10^exponent
.The return value of this function is undefined for exponent values < 0.
Decimal.tenToThePowerOf(3) == 1000;
let normalize: t => t => (t, t, int);
Decimal.normalize
normalizes the exponent to the minimal exponent for two givenDecimal
values.let a = Decimal(12345, -2); // 123.45 let b = Decimal(12345, 3); // 12345000 let res = Decimal.normalize(a, b); res == (Decimal(12345, -2), Decimal(1234500000, -2), -2)
let add: t => t => t;
Decimal.add
adds twoDecimal
values with no attempt at avoiding overflow.Note: the arguments are in order of
lhs
,rhs
.