LEVIATHAN vd52e79c4 · d52e79c486

Standard Library

class decimal

An exact base-10 number: up to 34 significant digits and up to 34 digits after the point.

since 0.1.0-alpha.1linuxwindowswasm

Overview

A decimal is a coefficient, a sign and a scale, the number of digits after the point. 0.1 + 0.2 is exactly 0.3, which is why prices, ledgers and other base-10 quantities use decimal rather than float. The scale is kept: 9.90 prints as 9.90, and 2 * 9.90 as 19.80. Equality is by value, so 1.0 == 1.00 is true, and 1.0 and 1.00 are the same Map key.

A literal is a decimal when its context is (decimal price = 19.99;), and anywhere when it ends in m (19.99m). Either way it is built exactly from its digits, never through a float. A decimal mixes only with decimal: convert an int or a float with toDecimal, and convert back with toInt or toFloat.

+, -, * and % are exact unless the result needs more than 34 digits. / keeps the dividend's scale less the divisor's when the quotient is exact, and gives 34 significant digits when it is not. Every rounding the operators do is half to even. A result too large for 34 digits throws decimal overflow; dividing by zero throws division by zero.

Examples

Typical use

use io::console;

decimal price = 19.99;
int qty = 3;
decimal total = price * qty.toDecimal();
console.writeln(total);
console.writeln(0.1m + 0.2m == 0.3m);
console.writeln(total / 4);
console.writeln((total / 4).round(2));
console.writeln(1m / 3);
console.writeln("total: ${total}");
59.97
true
14.9925
14.99
0.3333333333333333333333333333333333
total: 59.97

Methods

abs

abs() -> decimal

Return the absolute value, with the scale kept.

Returns

this without its sign.

Examples

use io::console;

console.writeln((-12.50m).abs());
console.writeln((3m).abs());
12.50
3

ceil

ceil() -> decimal

Round up to a whole number (toward positive infinity).

Returns

The smallest whole number not less than this, with scale 0.

Examples

use io::console;

console.writeln((-1.25m).ceil());
console.writeln((1.25m).ceil());
-1
2

See also: floor, trunc

clamp

clamp(decimal lo, decimal hi) -> decimal

Limit the number to the range from lo to hi.

Parameters

lo
The smallest value to return.
hi
The largest value to return.

Returns

lo if this is below it, hi if this is above it, otherwise this.

Throws

RuntimeException
when lo is greater than hi.

Examples

use io::console;

console.writeln((9.99m).clamp(0m, 5m));
console.writeln((-0.5m).clamp(0m, 5m));
console.writeln((2.50m).clamp(0m, 5m));
try {
    console.writeln((1m).clamp(2m, 0m));
} catch (RuntimeException e) {
    console.writeln("caught: ${e.message}");
}
5
0
2.50
caught: clamp: lo > hi

floor

floor() -> decimal

Round down to a whole number (toward negative infinity).

Returns

The largest whole number not greater than this, with scale 0.

Examples

use io::console;

console.writeln((-1.25m).floor());
console.writeln((1.25m).floor());
console.writeln((3.00m).floor());
-2
1
3

See also: ceil, trunc

max

max(decimal other) -> decimal

Return the larger of two numbers.

When the two are equal by value, other is returned, with its scale.

Parameters

other
The value to compare with.

Returns

this if it is greater than other, otherwise other.

Examples

use io::console;

console.writeln((1.5m).max(2.25m));
console.writeln((-1m).max(-3m));
console.writeln((1.0m).max(1.00m));
2.25
-1
1.00

See also: min

min

min(decimal other) -> decimal

Return the smaller of two numbers.

When the two are equal by value, other is returned, with its scale.

Parameters

other
The value to compare with.

Returns

this if it is less than other, otherwise other.

Examples

use io::console;

console.writeln((1.5m).min(2.25m));
console.writeln((-1m).min(-3m));
1.5
-3

See also: max

normalize

normalize() -> decimal

Remove trailing zeros after the point.

The result is the same value at the smallest scale that still represents it; zero becomes 0. Zeros before the point are kept.

Returns

this with no trailing fraction zeros.

Examples

use io::console;

console.writeln((1.50m).normalize());
console.writeln((100m).normalize());
console.writeln((0.000m).normalize());
1.5
100
0

See also: scale

pow

pow(int n) -> decimal

Raise the number to an integer power.

The power is computed by repeated squaring with decimal *, so each step is exact unless it needs more than 34 digits. A negative exponent gives 1 / x.pow(-n). x.pow(0) is 1 for every x, including 0.

Parameters

n
The exponent; it may be negative.

Returns

this to the power n.

Throws

RuntimeException
decimal overflow when the result is too large, and division by zero for a negative power of zero.

Examples

use io::console;

console.writeln((1.10m).pow(2));
console.writeln((2m).pow(-2));
console.writeln((0m).pow(0));
try {
    console.writeln((0m).pow(-1));
} catch (RuntimeException e) {
    console.writeln("caught: ${e.message}");
}
1.2100
0.25
1
caught: division by zero

round

round() -> decimal

Round to a whole number, a tie going to the even neighbour.

This is round(0, RoundingMode::HalfEven): 2.5 becomes 2, 3.5 becomes 4. Note that float.round rounds ties away from zero instead.

Returns

The nearest whole number, with scale 0.

Examples

use io::console;

console.writeln((2.5m).round());
console.writeln((3.5m).round());
console.writeln((-2.5m).round());
console.writeln((2.51m).round());
2
4
-2
3
round(int places) -> decimal

Round to places digits after the point, a tie going to the even neighbour.

When this has fewer digits after the point, the result is padded with zeros to places digits (as far as 34 significant digits allow), so the result always prints with places digits.

Parameters

places
The digits to keep after the point, from 0 to 34.

Returns

this rounded to places digits.

Throws

RuntimeException
round: places must be in 0..34 when places is out of range.

Examples

use io::console;

console.writeln((2.345m).round(2));
console.writeln((2.355m).round(2));
console.writeln((1.5m).round(3));
try {
    console.writeln((1.5m).round(35));
} catch (RuntimeException e) {
    console.writeln("caught: ${e.message}");
}
2.34
2.36
1.500
caught: round: places must be in 0..34
round(int places, RoundingMode mode) -> decimal

Round to places digits after the point by the rule of mode.

Parameters

places
The digits to keep after the point, from 0 to 34.
mode
How to round when digits are dropped.

Returns

this rounded to places digits.

Throws

RuntimeException
round: places must be in 0..34 when places is out of range.

Examples

use io::console;

console.writeln((2.345m).round(2, RoundingMode::HalfAwayFromZero));
console.writeln((2.341m).round(2, RoundingMode::Ceiling));
console.writeln((-2.349m).round(2, RoundingMode::TowardZero));
console.writeln((7.5m).round(0, RoundingMode::HalfTowardZero));
2.35
2.35
-2.34
7

See also: RoundingMode, floor

scale

scale() -> int

The number of digits after the point.

The scale is part of the value's text but not of its equality: 1.0 and 1.00 are equal and have scales 1 and 2.

Returns

The scale, from 0 to 34.

Examples

use io::console;

console.writeln((9.90m).scale());
console.writeln((5m).scale());
console.writeln((1.10m * 1.10m).scale());
2
0
4

See also: normalize

sign

sign() -> int

Return the sign of the number.

Returns

-1 if this is negative, 0 if it is zero, and 1 if it is positive.

Examples

use io::console;

console.writeln((-0.5m).sign());
console.writeln((0.00m).sign());
console.writeln((3m).sign());
-1
0
1

toFloat

toFloat() -> float

Convert to the nearest float.

A tie between two float values goes to the one with the even last bit. Every decimal is in range, so this never throws, but a float holds only about 16 significant digits.

Returns

The float nearest to this.

Examples

use io::console;

console.writeln((0.1m).toFloat());
console.writeln((1m / 3).toFloat());
console.writeln((0.1m).toFloat() == 0.1);
1.0e-1
3.333333333333333e-1
true

See also: toDecimal

toInt

toInt() -> int

Convert to an int, dropping any fraction (truncation toward zero).

Returns

The integer part of this.

Throws

RuntimeException
decimal out of int64 range for toInt() when the integer part does not fit in an int.

Examples

use io::console;

console.writeln((19.99m).toInt());
console.writeln((-19.99m).toInt());
decimal big = 100000000000000000000;
try {
    console.writeln(big.toInt());
} catch (RuntimeException e) {
    console.writeln("caught: ${e.message}");
}
19
-19
caught: decimal out of int64 range for toInt()

See also: trunc

toString

@Pure
toString() -> string

Format the number as plain text with exactly scale() digits after the point.

The text never uses an exponent: a negative value starts with -, a value below 1 starts with 0, and a value with scale 0 has no point. It is also what string concatenation, ${} interpolation and console.writeln produce, and string.toDecimal reads it back to the same value and scale.

Returns

The digits of this, with its scale.

Examples

use io::console;

console.writeln((9.90m).toString());
console.writeln((2 * 9.90m).toString());
console.writeln((-0.0050m).toString());
console.writeln("$" + 5m.toString());
9.90
19.80
-0.0050
$5

See also: toDecimal

trunc

trunc() -> decimal

Drop the fraction (round toward zero).

Returns

The integer part of this as a decimal with scale 0.

Examples

use io::console;

console.writeln((-1.25m).trunc());
console.writeln((1.75m).trunc());
-1
1

See also: floor, toInt

See also

  • RoundingMode — How a decimal is rounded when digits are dropped.
  • float — A 64-bit IEEE 754 floating-point number (binary64).
  • toDecimal — Convert the integer to a decimal, exactly.