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 decimal

An 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.5

decimal.of: inputs flowing into the function and the resultvalue"1.50"decimal.ofresult · decimal1.5value"19.99"decimal.ofresult · decimal19.99value0.1decimal.ofresult · decimal0.1

Real result: worked out by running this example with TB while the site was built.

is_decimal(x)

returns bool

True if x came from the decimal library.

Visualizehow decimal.is_decimal works

decimal.is_decimal(decimal.of("1")) gives true

decimal.is_decimal: inputs flowing into the function and the resultxdecimal.of("1")decimal.is_decimalresult · booltruex1decimal.is_decimalresult · boolfalsex"1"decimal.is_decimalresult · boolfalse

Real result: worked out by running this example with TB while the site was built.

add(a, b)

returns decimal

a + 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: inputs flowing into the function and the resulta"0.1"b"0.2"decimal.addresult · decimal"0.3"a"0.1"b"0.25"decimal.addresult · decimal"0.35"a"0.1"b"-0.1"decimal.addresult · decimal"0.0"

Real result: worked out by running this example with TB while the site was built.

sub(a, b)

returns decimal

a - b exactly.

Visualizehow decimal.sub works

decimal.sub("1.00", "0.35").text gives "0.65"

decimal.sub: inputs flowing into the function and the resulta"1.00"b"0.35"decimal.subresult · decimal"0.65"

Real result: worked out by running this example with TB while the site was built.

mul(a, b)

returns decimal

a * b exactly.

Visualizehow decimal.mul works

decimal.mul("19.99", 3).text gives "59.97"

decimal.mul: inputs flowing into the function and the resulta"19.99"b3decimal.mulresult · decimal"59.97"

Real result: worked out by running this example with TB while the site was built.

div(a, b)

returns decimal

a / b to 34 significant digits. Error on division by zero.

Visualizehow decimal.div works

decimal.div("1", "3").text gives "0.3333333333333333333333333333333333"

decimal.div: inputs flowing into the function and the resulta"1"b"3"decimal.divresult · decimal"0.3333333333333333333333333333333333"a"1"b"4"decimal.divresult · decimal"0.25"a"1"b"0"decimal.divstops with an errorYou 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 decimal

Round 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: inputs flowing into the function and the resultvalue"2.675"places2mode?"half_up"decimal.roundresult · decimal"2.68"value"2.675"places2mode?"half_even"decimal.roundresult · decimal"2.68"value"2.675"places2mode?"down"decimal.roundresult · decimal"2.67"value"2.675"places2mode?"up"decimal.roundresult · decimal"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 0

decimal.compare: inputs flowing into the function and the resulta"1.5"b"1.50"decimal.compareresult · number0a"1.5"b"2"decimal.compareresult · number-1a"1.5"b"1.4"decimal.compareresult · number1

Real result: worked out by running this example with TB while the site was built.

sum(values, ...)

returns decimal

Add 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"

decimal.sum: inputs flowing into the function and the resultvalues"0.1""0.2"0.3decimal.sumresult · decimal"0.6"

Real result: worked out by running this example with TB while the site was built.

text(value)

returns text

A decimal (or number) as plain digits: decimal.text(1e21) has no exponent.

Visualizehow decimal.text works

decimal.text(1e21) gives "1000000000000000000000"

decimal.text: inputs flowing into the function and the resultvalue1e+21decimal.textresult · text"1000000000000000000000"valuedecimal.of("0.000001")decimal.textresult · text"0.000001"value2.5decimal.textresult · text"2.5"

Real result: worked out by running this example with TB while the site was built.

number(value)

returns number

The nearest plain number for a decimal.

Visualizehow decimal.number works

decimal.number(decimal.of("19.99")) gives 19.99

decimal.number: inputs flowing into the function and the resultvaluedecimal.of("19.99")decimal.numberresult · number19.99

Real result: worked out by running this example with TB while the site was built.

money(value, places?, mode?)

returns text

Round 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", 2) gives "1,234.50"

decimal.money: inputs flowing into the function and the resultvalue"1234.5"places?0mode?left outdecimal.moneyresult · text"1,235"value"1234.5"places?2mode?left outdecimal.moneyresult · text"1,234.50"value"1234.5"places?3mode?left outdecimal.moneyresult · text"1,234.500"

Real result: worked out by running this example with TB while the site was built.

Values

NameValueWhat it is
decimal.precision34

How many significant digits arithmetic keeps (34).

Visualizehow decimal.precision works

decimal.precision gives 34

The value decimal.precisiondecimal.precision34what it isHow many significant digits arithmetickeeps (34).

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

decimal.modes gives ["half_even", "half_up", "half_down", "up", "down", "ceiling", "floor"]

The value decimal.modesdecimal.modes"half_even""half_up""half_down""up""down""ceiling""floor"what it isThe rounding modes round() accepts.

No inputs: call it as it is.

Real value, read from TB while the site was built.