Grav v1.14.0

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"))
}
Output
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.90 prints as 19.90, though == and the other comparisons find it equal to 19.9.
  • + and - give the larger of the two scales, * their sum (1.10 * 2.5 is 2.750). Division needs the scale you want: a.div(b, 2). div and round(n) round half away from zero (2.345 to 2.35, -2.345 to -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), and Decimal.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 number n (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 — For x as Decimal: a whole number, a float (its shortest decimal) or text (parsed).
  • static fn from(n: long) -> Decimal
  • static fn from(x: float) -> Decimal
  • static fn from(s: string) -> Decimal
  • static 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 float x as 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) -> int
  • static 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) -> bool
  • static fn min(a: Decimal, b: Decimal) -> Decimal
  • static fn max(a: Decimal, b: Decimal) -> Decimal
  • static fn sum(xs: Array<Decimal>) -> Decimal — The total of xs (0 for none), exactly.
  • static fn average(xs: Array<Decimal>, scale: int) -> Decimal — Their mean, to scale digits; ArithmeticError for none.

Methods

  • fn scale() -> int — Digits after the point.
  • fn sign() -> int — -1, 0 or 1.
  • fn isZero() -> bool
  • fn negate() -> Decimal
  • fn abs() -> Decimal
  • fn operator+(o: Decimal) -> Decimal
  • fn operator-(o: Decimal) -> Decimal
  • fn operator*(o: Decimal) -> Decimal
  • fn operator/(o: Decimal) -> Decimal
  • fn operator%(o: Decimal) -> Decimal
  • fn operator+(n: long) -> Decimal
  • fn operator-(n: long) -> Decimal
  • fn operator*(n: long) -> Decimal
  • fn operator/(n: long) -> Decimal
  • fn operator%(n: long) -> Decimal
  • fn dividedBy(o: Decimal) -> Decimal — This divided by o: 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 of o (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 least keep digits after the point.
  • fn floor() -> Decimal — Rounded down (toward minus infinity) and up to whole numbers.
  • fn ceil() -> Decimal
  • fn trunc() -> Decimal — Its whole part, toward zero (-2.7 -> -2), and what is left.
  • fn fraction() -> Decimal
  • fn pow(e: int) -> Decimal — This to the power e (e >= 0), exactly; ArithmeticError past 38 digits.
  • fn isWhole() -> bool — Whether it has no fraction (5.00 has none).
  • fn percent(p: Decimal) -> Decimal — p percent of this (Decimal.of(200).percent(Decimal.of(15)) is 30).
  • fn plus(o: Decimal) -> Decimal
  • fn minus(o: Decimal) -> Decimal
  • fn times(o: Decimal) -> Decimal
  • fn div(o: Decimal, scale: int) -> Decimal — This divided by o, rounded to scale digits; ArithmeticError for 0.
  • fn round(scale: int) -> Decimal — Rounded (or padded with zeros) to scale digits 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 than o (19.9 equals 19.90).
  • fn operator==(o: Decimal) -> bool
  • fn operator<(o: Decimal) -> bool
  • fn operator<=(o: Decimal) -> bool
  • fn operator>(o: Decimal) -> bool
  • fn operator>=(o: Decimal) -> bool
  • fn operator==(n: long) -> bool
  • fn operator<(n: long) -> bool
  • fn operator<=(n: long) -> bool
  • fn operator>(n: long) -> bool
  • fn operator>=(n: long) -> bool
  • fn clamp(lo: Decimal, hi: Decimal) -> Decimal — x kept 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.

Edit this page on GitHub