LEVIATHAN vd52e79c4 · d52e79c486

Standard Library

enum RoundingMode

How a decimal is rounded when digits are dropped.

since 0.1.0-alpha.1linuxwindowswasm

Overview

Every rounding method of decimal takes a mode: round(places, mode) rounds to places digits after the point by the mode's rule. Division and results longer than 34 digits always round HalfEven, as do round() and round(places). floor, ceil and trunc are round(0, Floor), round(0, Ceiling) and round(0, TowardZero).

The codes run from 0 (HalfEven) to 6 (Floor) in declaration order.

Examples

Typical use

use io::console;

decimal x = 2.345;
console.writeln(x.round(2));
console.writeln(x.round(2, RoundingMode::HalfAwayFromZero));
console.writeln(x.round(2, RoundingMode::Ceiling));
console.writeln((0m - x).round(2, RoundingMode::Floor));
console.writeln(RoundingMode::Floor.code());
console.writeln(RoundingMode::HalfEven.toString());
2.34
2.35
2.35
-2.35
6
HalfEven

Members

AwayFromZero

AwayFromZero = 3

Round away from zero unless the dropped digits are all zero. 2.1 becomes 3 and -2.1 becomes -3.

Ceiling

Ceiling = 5

Round toward positive infinity. 2.1 becomes 3 and -2.9 becomes -2.

Floor

Floor = 6

Round toward negative infinity. 2.9 becomes 2 and -2.1 becomes -3.

HalfAwayFromZero

HalfAwayFromZero = 1

Round to the nearest value; a tie goes away from zero. 2.5 becomes 3 and -2.5 becomes -3.

HalfEven

HalfEven = 0

Round to the nearest value; a tie goes to the neighbour whose last digit is even (banker's rounding). 2.5 becomes 2 and 3.5 becomes 4. The default everywhere.

HalfTowardZero

HalfTowardZero = 2

Round to the nearest value; a tie goes toward zero. 2.5 becomes 2 and -2.5 becomes -2.

TowardZero

TowardZero = 4

Drop the digits (truncate toward zero). 2.9 becomes 2 and -2.9 becomes -2.

See also

  • round — Round to a whole number, a tie going to the even neighbour.
  • decimal — An exact base-10 number: up to 34 significant digits and up to 34 digits after the point.