This library provides arbitrary precision 'Decimal' type and basic arithmetic operations on it.
Significand is an alias for an Integer.
Exponent is an alias for Int.
minExponent specifies the default minimum exponent. It's currently -32, chosen arbitrarily.
Decimal can be constructed from Int, Float, String, or Integer.
Construct Decimal from Int.
fromInt 89928271371 -- Decimal (Integer Positive [8271371,8992]) 0 : Decimal
fromInt 0 -- Decimal Integer.Zero 0 : Decimal
fromInt -899210300 -- Decimal (Integer Negative [8992103]) 2 : Decimal
Entering numbers that cannot be represented by Javascript number type will result in incorrect representation. Use fromString in that case.
fromInt 81927398127398127389172893712893712893798123 |> toString -- "-10497034289617408" : String
fromString "81927398127398127389172893712893712893798123" |> Maybe.map toString -- Just "81927398127398127389172893712893712893798123" : Maybe StringConstruct Decimal from Float.
fromFloat -8213.211 -- Decimal (Integer Negative [8213211]) -3 : Decimal
fromFloat 0.0000000000000000000000000000000003323232238 -- Decimal (Integer Positive [3]) -34 : Decimal
Entering numbers that cannot be represented by Javascript number type will result in incorrect representation. Use fromString in that case.
fromFloat -1727816283761287361287631287.123123123123 |> toString -- "-1727816283761287400000000000" : String
fromString "-1727816283761287361287631287.123123123123" |> Maybe.map toString -- Just "-1727816283761287361287631287.123123123123" : Maybe StringConstruct Decimal from String.
fromString "0.0000000076128736" -- Just (Decimal (Integer Positive [6128736,7]) -16) : Maybe Decimal
fromString "-7637370.761287360000" -- Just (Decimal (Integer Negative [6128736,6373707,7]) -8) : Maybe Decimal
fromString "ajsdlkfj" -- Nothing : Maybe DecimalConstruct Decimal from Integer.
fromInteger (Integer.fromString "812371263726178361298371987329810000" |> Maybe.withDefault Integer.zero) -- Decimal (Integer Positive [8732981,2983719,6178361,7126372,8123]) 4 : Decimal
fromInteger Integer.zero -- Decimal Integer.Zero 0 : Decimal
fromInteger (Integer.negate Integer.zillion) -- Decimal (Integer Negative [1]) 12 : DecimalGet significand portion of Decimal.
Add two Decimals.
add (fromFloat 8172398.121) (fromFloat 0.0000001) |> toString -- "8172398.1210001" : String
add (fromFloat 0.000032) (fromFloat -0.000032) |> toString -- "0" : String
add (fromFloat -0.002) (fromFloat 0) |> toString -- "-0.002" : StringSubtract two Decimals.
sub (fromFloat 8172398.121) (fromFloat 0.0000001) |> toString -- "8172398.1209999" : String
sub (fromFloat 0.000032) (fromFloat -0.000032) |> toString -- "0.000064" : String
sub (fromFloat -0.002) (fromFloat 0) |> toString -- "-0.002" : StringMultiply two Decimals.
mul (fromFloat 8172398.121) (fromFloat 0.0000001) |> toString -- "0.8172398121" : String
mul (fromFloat 0.000032) (fromFloat -0.000032) |> toString -- "-0.000000001024" : String
mul (fromFloat -0.002) (fromFloat 0) |> toString -- "0" : StringUse this function when you want a multiplication with more fraction digits than what can be expressed with minExponent.
Divide two Decimals.
div (fromFloat 8172398.121) (fromFloat 0.0000001) |> Maybe.map toString -- Just "81723981210000" : Maybe String
div (fromFloat 0.000032) (fromFloat -0.000032) |> Maybe.map toString -- Just "-1" : Maybe String
div (fromFloat 0) (fromFloat -0.002) |> Maybe.map toString -- Just "0" : Maybe String
div (fromFloat -0.002) (fromFloat 0) |> Maybe.map toString -- Nothing : Maybe String
Maybe.andThen (div (fromInt 1)) (fromString "100000000000000000000000000000000000000") |> Maybe.map toString -- Nothing : Maybe StringUse this function when you want a division with more fraction digits than what can be expressed with minExponent.
Notation type determines how to represent Decimal as String. Dec represents decimal notation such as 0.003, 1231.6161, 335, or 33500. Sci represents scientific notation such as 3e-3, 1.2316161e3, or 3.35e4.
Represent Decimal as String. Default notation is Dec.
mul (fromFloat 8172387.1123) (fromFloat -3.3532) |> toString == "-27403648.46496436" : StringRepresent Decimal as String in desired notation.
Negate the sign of Decimal.
negate (fromInt 1232139812) |> toString == "-1232139812" : String
negate (fromFloat -127.1232139812) |> toString == "127.1232139812" : String
negate (fromInt 0) |> toString == "0" : StringGet absolute value of Decimal.
abs (fromInt 1232139812) |> toString == "1232139812" : String
abs (fromInt 0) |> toString == "0" : String
abs (fromFloat -127.1232139812) |> toString == "127.1232139812" : StringCompare two Decimals.
compare (fromFloat 23.223) (fromFloat 23.224) == LT : Order
compare (fromFloat 23.223) (fromFloat 23.22) == GT : Order
compare (fromFloat 23.223) (fromFloat -23.22) == GT : Order
compare (fromFloat 23.223) (fromFloat 23.223) == EQ : OrderSpecify a RoundingContext to customize the rounding strategy.
RoundingContext type can be used to specify rounding strategy. Specify e to designate the digit you would like to round to, and choose a mode for rounding. e of 0 will round to an integer; e of -1 will round to the nearest tenths, e of -2 to the nearest hundredths; e of 1 will round to tens, e of 2 to hundreds.
RoundingMode type determines rounding strategy. Default strategy is HalfToEven.
Value | Down | Up | TowardsZero | AwayFromZero | HalfToEven +1.8 | +1 | +2 | +1 | +2 | +2 +1.5 | +1 | +2 | +1 | +2 | +2 +1.2 | +1 | +2 | +1 | +2 | +1 +0.8 || 0 | +1 || 0 | +1 | +1 +0.5 || 0 | +1 || 0 | +1 || 0 +0.2 || 0 | +1 || 0 | +1 || 0 −0.2 | −1 || 0 || 0 | −1 || 0 −0.5 | −1 || 0 || 0 | −1 || 0 −0.8 | −1 || 0 || 0 | -1 | -1 −1.2 | −2 | −1 | −1 | −2 | −1 −1.5 | −2 | −1 | −1 | −2 | −2 −1.8 | −2 | −1 | −1 | −2 | −2
Use this function when you want a square root with more fraction digits than what can be expressed with minExponent.
A
Decimalis an arbitrary precision number with significand and exponent.