math.decimal #
Description
math.decimal provides arbitrary-precision fixed-point decimal arithmetic. It stores values as an integer coefficient plus a decimal scale, so addition, subtraction, and multiplication stay exact.
Division is exposed through div_prec, which rounds half up to a caller-chosen number of decimal places. The / operator uses decimal.default_division_precision.
Example
import math.decimal
price := decimal.from_string('19.99')!
fee := decimal.from_string('1.50')!
total := price + fee
assert total.str() == '21.49'
share := total.div_prec(decimal.from_int(3), 2)!
assert share.str() == '7.16'
Constants #
const default_division_precision = 16
default_division_precision is used by the / operator when the quotient needs a rounded decimal expansion.
const zero = Decimal{
coefficient_value: big.zero_int
scale_value: 0
}
zero is the canonical zero value for Decimal.
fn from_i64 #
fn from_i64(value i64) Decimal
from_i64 creates a decimal from an i64.
fn from_int #
fn from_int(value int) Decimal
from_int creates a decimal from an int.
fn from_string #
fn from_string(input string) !Decimal
from_string parses a decimal string without exponent notation.
fn from_u64 #
fn from_u64(value u64) Decimal
from_u64 creates a decimal from a u64.
fn new #
fn new(coefficient big.Integer, scale int) Decimal
new creates a decimal from coefficient * 10^-scale.
struct Decimal #
struct Decimal {
coefficient_value big.Integer
scale_value int
}
Decimal represents an arbitrary-precision fixed-point decimal value. Its numeric value is coefficient * 10^-scale.
fn (Decimal) coefficient #
fn (d Decimal) coefficient() big.Integer
coefficient returns the integer coefficient stored by d.
fn (Decimal) scale #
fn (d Decimal) scale() int
scale returns the number of decimal places stored by d.
fn (Decimal) is_zero #
fn (d Decimal) is_zero() bool
is_zero returns true when d == 0.
fn (Decimal) abs #
fn (d Decimal) abs() Decimal
abs returns the absolute value of d.
fn (Decimal) neg #
fn (d Decimal) neg() Decimal
neg returns the negated value of d.
fn (Decimal) str #
fn (d Decimal) str() string
str returns the canonical decimal representation of d.
fn (Decimal) + #
fn (left Decimal) + (right Decimal) Decimal
- returns the exact sum of
leftandright.
fn (Decimal) - #
fn (left Decimal) - (right Decimal) Decimal
- returns the exact difference of
leftandright.
fn (Decimal) * #
fn (left Decimal) * (right Decimal) Decimal
- returns the exact product of
leftandright.
fn (Decimal) / #
fn (dividend Decimal) / (divisor Decimal) Decimal
/ divides dividend by divisor using default_division_precision.
fn (Decimal) div_prec #
fn (dividend Decimal) div_prec(divisor Decimal, precision int) !Decimal
div_prec divides dividend by divisor and rounds half up to precision digits after the decimal point.
fn (Decimal) == #
fn (left Decimal) == (right Decimal) bool
== returns true when left and right represent the same value.
fn (Decimal) < #
fn (left Decimal) < (right Decimal) bool
< returns true when left is smaller than right.