Decimal
import "std/types" (included in import "std") · namespace std.types
Decimal is an exact decimal number, for money and for databases' DECIMAL and NUMERIC columns, where a float's binary rounding will not do: 0.1 + 0.2 is 0.30000000000000004 as floats and exactly 0.3 as Decimals.
grav
import "std"
fn main() {
let price = Decimal.parse("19.99")
let total = price * Decimal.of(3)
print(total)
print(total + Decimal.parse("0.01"))
print(Decimal.fromFloat(0.1) + Decimal.fromFloat(0.2))
print(Decimal.of(1).div(Decimal.of(3), 4))
print(Decimal.parse("2.345").round(2))
print(Decimal.parse("19.90") == Decimal.parse("19.9"))
}59.97
59.98
0.3
0.3333
2.35
true- A Decimal holds up to 38 significant digits, with up to 38 of them after the point: its scale. It keeps the scale it was written with, so
19.90prints as19.90, though==and the other comparisons find it equal to19.9. +and-give the larger of the two scales,*their sum (1.10 * 2.5is2.750). Division needs the scale you want:a.div(b, 2).divandround(n)round half away from zero (2.345to2.35,-2.345to-2.35).- Making one:
Decimal.parse("19.99")(also-0.5,+3,.25,1.5e3),Decimal.tryParse(null instead of a ValueError),Decimal.of(42),Decimal.of(1999, 2)(19.99), andDecimal.fromFloat(0.1), the shortest decimal that reads back as that float. - Getting a number back:
toString()writes every digit of its scale,toFloat()gives the nearest float,toLong()the whole part toward zero. - A result past 38 digits, and division by zero, are
ArithmeticErrors.
API
class Decimal
An exact decimal number (see the top of this file).
Constructors
new Decimal(hi: long, lo: long, scale: int)
Static members
static fn of(n: long) -> Decimal— The whole numbern(scale 0).static fn of(units: long, scale: int) -> Decimal—units× 10^-scale: Decimal.of(1999, 2) is 19.99.static fn from(n: int) -> Decimal— Forx as Decimal: a whole number, a float (its shortest decimal) or text (parsed).static fn from(n: long) -> Decimalstatic fn from(x: float) -> Decimalstatic fn from(s: string) -> Decimalstatic fn parse(text: string) -> Decimal— Parses "19.99", "-0.5", "+3", ".25" or "1.5e3"; ValueError for anything else, or for more than 38 digits.static fn tryParse(text: string) -> Decimal— As parse, but null when the text is not one.static fn fromFloat(x: float) -> Decimal— The floatxas the shortest decimal that reads back as it (0.1 is 0.1, not 0.1000000000000000055...); ValueError for NaN and infinities.static fn compare(a: Decimal, b: Decimal) -> int— Comparators for sorting (prices.sortBy(Decimal.compare)): -1, 0 or 1.static fn descending(a: Decimal, b: Decimal) -> intstatic fn equals(a: Decimal, b: Decimal) -> bool— Whether both are the same number (19.9 and 19.90 are), or the same number and scale.static fn identical(a: Decimal, b: Decimal) -> boolstatic fn min(a: Decimal, b: Decimal) -> Decimalstatic fn max(a: Decimal, b: Decimal) -> Decimalstatic fn sum(xs: Array<Decimal>) -> Decimal— The total ofxs(0 for none), exactly.static fn average(xs: Array<Decimal>, scale: int) -> Decimal— Their mean, toscaledigits; ArithmeticError for none.
Methods
fn scale() -> int— Digits after the point.fn sign() -> int— -1, 0 or 1.fn isZero() -> boolfn negate() -> Decimalfn abs() -> Decimalfn operator+(o: Decimal) -> Decimalfn operator-(o: Decimal) -> Decimalfn operator*(o: Decimal) -> Decimalfn operator/(o: Decimal) -> Decimalfn operator%(o: Decimal) -> Decimalfn operator+(n: long) -> Decimalfn operator-(n: long) -> Decimalfn operator*(n: long) -> Decimalfn operator/(n: long) -> Decimalfn operator%(n: long) -> Decimalfn dividedBy(o: Decimal) -> Decimal— This divided byo: 16 digits after the point (more when either has more), without trailing zeros past the larger of their scales (10 / 4 is 2.5, 1 / 3 is 0.3333333333333333); ArithmeticError for 0.fn remainder(o: Decimal) -> Decimal— What is left of this after taking out whole multiples ofo(with this one's sign: -7 % 3 is -1); ArithmeticError for 0.fn stripTrailingZeros() -> Decimal— The same number without trailing zeros after the point ("19.900" -> "19.9", "5.00" -> "5").fn stripTrailingZeros(keep: int) -> Decimal— As stripTrailingZeros, keeping at leastkeepdigits after the point.fn floor() -> Decimal— Rounded down (toward minus infinity) and up to whole numbers.fn ceil() -> Decimalfn trunc() -> Decimal— Its whole part, toward zero (-2.7 -> -2), and what is left.fn fraction() -> Decimalfn pow(e: int) -> Decimal— This to the powere(e >= 0), exactly; ArithmeticError past 38 digits.fn isWhole() -> bool— Whether it has no fraction (5.00 has none).fn percent(p: Decimal) -> Decimal—ppercent of this (Decimal.of(200).percent(Decimal.of(15)) is 30).fn plus(o: Decimal) -> Decimalfn minus(o: Decimal) -> Decimalfn times(o: Decimal) -> Decimalfn div(o: Decimal, scale: int) -> Decimal— This divided byo, rounded toscaledigits; ArithmeticError for 0.fn round(scale: int) -> Decimal— Rounded (or padded with zeros) toscaledigits after the point, half away from zero.fn compareTo(o: Decimal) -> int— -1, 0 or 1 as this is less than, equal to or greater thano(19.9 equals 19.90).fn operator==(o: Decimal) -> boolfn operator<(o: Decimal) -> boolfn operator<=(o: Decimal) -> boolfn operator>(o: Decimal) -> boolfn operator>=(o: Decimal) -> boolfn operator==(n: long) -> boolfn operator<(n: long) -> boolfn operator<=(n: long) -> boolfn operator>(n: long) -> boolfn operator>=(n: long) -> boolfn clamp(lo: Decimal, hi: Decimal) -> Decimal—xkept within lo..hi.fn toString() -> string— The number written out with all its scale's digits ("19.90", "-0.05", "42").fn toFloat() -> float— The nearest float.fn toInt() -> int— The whole part as an int, toward zero; ArithmeticError past an int's range.fn toLong() -> long— The whole part, toward zero (19.99 gives 19); ArithmeticError past a long's range.