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); // 6789000type t=
;|Decimal(mantissa, exponent)Represents an arbitrary precision number, backed by an integer mantissa, and integer exponent.
type rounding=
;|Truncate|RoundUp|RoundDown|RoundUpOrDownRepresents a preference for rounding a
Decimalvalue, when applicable.
let make: int => int => t;Decimal.makeconstructs aDecimalfrom amantissaandexponent.
let fromInt: int => t;Decimal.fromIntconstructs aDecimalfrom anint.
let show: t => string;Decimal.fromStringattempts to parse aDecimalfrom astringDecimal.showrenders theDecimalvalue to astring, as if theDecimalwas represented as afloat.Decimal(12345, -2) |> Decimal.toString // "123.45" Decimal(12345, 3) |> Decimal.toString // "12345000"
let tenToThePowerOfPositive: int => int;Decimal.roundrounds aDecimalusing the given rounding strategy.Decimal.tenToThePowerOfPositivecomputes 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.normalizenormalizes the exponent to the minimal exponent for two givenDecimalvalues.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.addadds twoDecimalvalues with no attempt at avoiding overflow.Note: the arguments are in order of
lhs,rhs.