decimal
Exact decimal numbers: decimal.of("0.1").add("0.2") is exactly 0.3, where plain numbers give 0.30000000000000004. Use them for money and anything that is counted in decimal places. A decimal has .text (what it shows), .number (nearest plain number) and methods add, sub, mul, div, round, compare, equals, abs, negate, is_zero; decimal.sum() adds a list. Arithmetic keeps 34 significant digits.
import decimal
Example
import decimal
say(0.1 + 0.2)
say(decimal.of("0.1").add("0.2").text)
let price = decimal.of("19.99")
say(price.mul(3).text, price.mul("1.18").round(2).text)
say(decimal.sum(["1.10", "2.20", "3.30"]).text)
Functions
of(value)
returns decimalAn exact decimal from text like "19.99", a number, or another decimal. Text keeps every digit ("1.50" stays 1.50); a plain number is read the way it prints (0.1 becomes exactly 0.1).
Visualizehow decimal.of works
decimal.of("1.50") gives 1.5decimal.of("19.99") gives 19.99decimal.of(0.1) gives 0.1
Real result: worked out by running this example with TB while the site was built.
is_decimal(x)
returns boolTrue if x came from the decimal library.
Visualizehow decimal.is_decimal works
decimal.is_decimal(decimal.of("1")) gives truedecimal.is_decimal(1) gives falsedecimal.is_decimal("1") gives false
Real result: worked out by running this example with TB while the site was built.
add(a, b)
returns decimala + b exactly: decimal.add("0.1", "0.2").text is "0.3".
Visualizehow decimal.add works
decimal.add("0.1", "0.2").text gives "0.3"decimal.add("0.1", "0.25").text gives "0.35"decimal.add("0.1", "-0.1").text gives "0.0"
Real result: worked out by running this example with TB while the site was built.
sub(a, b)
returns decimala - b exactly.
Visualizehow decimal.sub works
decimal.sub("1.00", "0.35").text gives "0.65"
Real result: worked out by running this example with TB while the site was built.
mul(a, b)
returns decimala * b exactly.
Visualizehow decimal.mul works
decimal.mul("19.99", 3).text gives "59.97"
Real result: worked out by running this example with TB while the site was built.
div(a, b)
returns decimala / b to 34 significant digits. Error on division by zero.
Visualizehow decimal.div works
decimal.div("1", "3").text gives "0.3333333333333333333333333333333333"decimal.div("1", "4").text gives "0.25"decimal.div("1", "0").text stops with an error: You can't divide by zero.
Real result: worked out by running this example with TB while the site was built.
round(value, places, mode?)
returns decimalRound to that many decimal places: decimal.round("2.675", 2).text is "2.68" with mode "half_up" (default "half_even" rounds ties to the even digit, like banks do).
Visualizehow decimal.round works
decimal.round("2.675", 2, "half_up").text gives "2.68"decimal.round("2.675", 2, "half_even").text gives "2.68"decimal.round("2.675", 2, "down").text gives "2.67"decimal.round("2.675", 2, "up").text gives "2.68"
Real result: worked out by running this example with TB while the site was built.
compare(a, b)
returns number-1 if a < b, 0 if equal, 1 if a > b. Handy for sort_by.
Visualizehow decimal.compare works
decimal.compare("1.5", "1.50") gives 0decimal.compare("1.5", "2") gives -1decimal.compare("1.5", "1.4") gives 1
Real result: worked out by running this example with TB while the site was built.
sum(values, ...)
returns decimalAdd up a list of decimals, numbers or texts exactly: decimal.sum(["0.1", "0.2", 0.3]).text is "0.6".
Visualizehow decimal.sum works
decimal.sum(["0.1", "0.2", 0.3]).text gives "0.6"
Real result: worked out by running this example with TB while the site was built.
text(value)
returns textA decimal (or number) as plain digits: decimal.text(1e21) has no exponent.
Visualizehow decimal.text works
decimal.text(1e21) gives "1000000000000000000000"decimal.text(decimal.of("0.000001")) gives "0.000001"decimal.text(2.5) gives "2.5"
Real result: worked out by running this example with TB while the site was built.
number(value)
returns numberThe nearest plain number for a decimal.
Visualizehow decimal.number works
decimal.number(decimal.of("19.99")) gives 19.99
Real result: worked out by running this example with TB while the site was built.
money(value, places?, mode?)
returns textRound like money and show with thousands separators and fixed places: decimal.money("1234.5") is "1,234.50". places defaults to 2, mode to "half_up".
Visualizehow decimal.money works
decimal.money("1234.5", 0) gives "1,235"decimal.money("1234.5", 2) gives "1,234.50"decimal.money("1234.5", 3) gives "1,234.500"
Real result: worked out by running this example with TB while the site was built.
Values
| Name | Value | What it is |
|---|---|---|
decimal.precision | 34 | How many significant digits arithmetic keeps (34). Visualizehow decimal.precision works
No inputs: call it as it is. Real value, read from TB while the site was built. |
decimal.modes | ["half_even", "half_up", "half_down", "up", "down", "ceiling", "floor"] | The rounding modes round() accepts. Visualizehow decimal.modes works
No inputs: call it as it is. Real value, read from TB while the site was built. |