Skip to content
FutureBlitz

    Install

    Import std.math.big.decimal

    Decimal

    std.math.big.decimal

    class

    Public Class Decimal Implements Comparable, Hashable, Printable, Numeric

    A decimal number, which holds what it's made from exactly, as 0.1 is, and keeps 28 significant digits in its arithmetic.

    Examples

    Local tenth = Decimal.Parse("0.1") ?? Decimal(0)
    Expect("{tenth + tenth + tenth}" == "0.3")
    Expect("{Decimal(1) / Decimal(3)}" == "0.3333333333333333333333333333")
    Local price = Decimal.Parse("2.675") ?? Decimal(0)
    Expect(price.Round(2).ToString() == "2.68")
    Expect(Decimal.Parse("1.50") == Decimal.Parse("1.5"))

    std/math/big/decimal.fb, line 75

    • Public New(value: Int64) constructor #

      The Decimal of value.

    • Public New(value: BigInt) constructor #

      The Decimal of value.

    • Public Static Method Zero(): Decimal static method #

      The zero Decimal, 0, which adding leaves alone.

    • Public Static Method One(): Decimal static method #

      The Decimal 1, which multiplying leaves alone.

    • @PanicsAtCaller Public Static Method FromDouble(value: Double): Decimal static method #

      The shortest Decimal that reads back as the Double, the digits it prints, so 0.1 is 0.1. A NaN or an infinity panics.

    • Public Static Method Parse(text: String): Decimal? static method #

      The number the text is, exactly: a sign or none, then digits with a "." or not, then an exponent or none, an "E" or "e" and an integer, as "-1.50" and "2.5e-3" are. Other text, NaN and the infinities too, is Null.

    • Public Method ToString(): String method #

      What Python's str gives: its digits with a point where it goes, as in "-1.50", unless its exponent is above 0 or it's quite small, as in "1.5E+3" and "1E-7".

    • Public Method ToPlainString(): String method #

      Its digits with no exponent, as in "1500" and "0.0000001".

    • Public Method ToDouble(): Double method #

      The Double nearest it, ties to even: an infinity past the largest Double, and 0 below the smallest.

    • Public Method ToInt64(): Int64? method #

      It as an Int64, or Null when it isn't a whole number in an Int64's range.

    • Public Method ToBigInt(): BigInt? method #

      It as a BigInt, or Null when it isn't a whole number.

    • Public Property Sign: Int property #

      -1, 0, or 1 as it's below, at, or above 0.

    • Public Property IsInteger: Bool property #

      Whether it's a whole number, as 2.00 and 1E+3 are.

    • Public Method Abs(): Decimal method #

      Its distance from 0: the same Decimal, without its sign.

    • @PanicsAtCaller Public Method Round(places: Int = 0, mode: RoundingMode = RoundingMode.HALF_EVEN): Decimal method #

      It rounded by the mode to the places after the point, or before it when negative: 2.675 is 2.68 to 2 places, and 1234 is 1.2E+3 to -2. Rounding to more digits than 28, or than it has when that's more, panics, and so does a result whose exponent is past 999999 either way, as with Python's quantize.

    • Public Method Truncate(): Decimal method #

      It rounded to a whole number, toward 0, down, or up. One with an exponent above 0 keeps it, as 1.5E+3 does.

    • Public Method Floor(): Decimal method #

      It rounded down to a whole number, toward negative infinity.

    • Public Method Ceiling(): Decimal method #

      It rounded up to a whole number, toward positive infinity.

    • @PanicsAtCaller Public Method Sqrt(): Decimal method #

      Its square root. One below 0 panics.

    • @PanicsAtCaller Public Method Exp(): Decimal method #

      e to its power.

    • @PanicsAtCaller Public Method Ln(): Decimal method #

      Its natural logarithm. Panics unless it's above 0.

    • @PanicsAtCaller Public Method Log10(): Decimal method #

      Its base 10 logarithm. Panics unless it's above 0.

    • @PanicsAtCaller Public Method Pow(exponent: Decimal): Decimal method #

      It to the power, which needn't be whole: 2 to 0.5 is the square root of 2. Anything to 0 is 1, 0 to a power below 0 panics, and so does a number below 0 to one that isn't whole.

    • Public Method Hash(): UInt64 method #

      A hash of its value, the same for Decimals that are equal, as 1.5 and 1.50 are.

    • Operator ==(a: Decimal, b: Decimal): Bool operator #

      Whether a and b are the same number, whatever trailing 0s either has.

    • Operator ==(a: Decimal, b: Int64): Bool operator #

      Whether a and b are equal.

    • Operator ==(a: Int64, b: Decimal): Bool operator #

      Whether a and b are equal.

    • Operator <(a: Decimal, b: Decimal): Bool operator #

      Whether a is less than b.

    • Operator <(a: Decimal, b: Int64): Bool operator #

      Whether a comes before b.

    • Operator <(a: Int64, b: Decimal): Bool operator #

      Whether a comes before b.

    • Operator >(a: Decimal, b: Int64): Bool operator #

      Whether a comes after b.

    • Operator >(a: Int64, b: Decimal): Bool operator #

      Whether a comes after b.

    • Operator <=(a: Decimal, b: Int64): Bool operator #

      Whether a comes before b or is equal to it.

    • Operator <=(a: Int64, b: Decimal): Bool operator #

      Whether a comes before b or is equal to it.

    • Operator >=(a: Decimal, b: Int64): Bool operator #

      Whether a comes after b or is equal to it.

    • Operator >=(a: Int64, b: Decimal): Bool operator #

      Whether a comes after b or is equal to it.

    • @PanicsAtCaller Operator +(a: Decimal, b: Decimal): Decimal operator #

      a plus b, to 28 significant digits. Panics when the result's exponent is past 999999.

    • @PanicsAtCaller Operator +(a: Decimal, b: Int64): Decimal operator #

      a plus b.

    • @PanicsAtCaller Operator +(a: Int64, b: Decimal): Decimal operator #

      a plus b.

    • @PanicsAtCaller Operator -(a: Decimal, b: Decimal): Decimal operator #

      a minus b, to 28 significant digits. Panics when the result's exponent is past 999999.

    • @PanicsAtCaller Operator -(a: Decimal, b: Int64): Decimal operator #

      a minus b.

    • @PanicsAtCaller Operator -(a: Int64, b: Decimal): Decimal operator #

      a minus b.

    • @PanicsAtCaller Operator *(a: Decimal, b: Decimal): Decimal operator #

      a times b, to 28 significant digits. Panics when the result's exponent is past 999999.

    • @PanicsAtCaller Operator *(a: Decimal, b: Int64): Decimal operator #

      a times b.

    • @PanicsAtCaller Operator *(a: Int64, b: Decimal): Decimal operator #

      a times b.

    • @PanicsAtCaller Operator /(a: Decimal, b: Decimal): Decimal operator #

      a divided by b, to 28 significant digits. A b of 0 panics.

    • @PanicsAtCaller Operator /(a: Decimal, b: Int64): Decimal operator #

      a divided by b.

    • @PanicsAtCaller Operator /(a: Int64, b: Decimal): Decimal operator #

      a divided by b.

    • @PanicsAtCaller Operator %(a: Decimal, b: Decimal): Decimal operator #

      What's left of a / b with its quotient rounded toward 0, so it has a's sign, exactly. A quotient of more digits than 28, or than a or b has when that's more, panics.

    • @PanicsAtCaller Operator %(a: Decimal, b: Int64): Decimal operator #

      What's left of a / b with its quotient rounded toward 0, so it has a's sign, exactly. A quotient of more digits than 28, or than a or b has when that's more, panics.

    • @PanicsAtCaller Operator %(a: Int64, b: Decimal): Decimal operator #

      What's left of a / b with its quotient rounded toward 0, so it has a's sign, exactly. A quotient of more digits than 28, or than a or b has when that's more, panics.

    • @PanicsAtCaller Operator **(a: Decimal, b: Int64): Decimal operator #

      To a whole power: anything to 0 is 1, and 0 to a power below 0 panics.

    • Operator -(a: Decimal): Decimal operator #

      a negated.

    Report an issue with this page or with FutureBlitz on GitHub.