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 = 3Round away from zero unless the dropped digits are all zero. 2.1 becomes 3 and -2.1 becomes -3.
Ceiling
Ceiling = 5Round toward positive infinity. 2.1 becomes 3 and -2.9 becomes -2.
Floor
Floor = 6Round toward negative infinity. 2.9 becomes 2 and -2.1 becomes -3.
HalfAwayFromZero
HalfAwayFromZero = 1Round to the nearest value; a tie goes away from zero. 2.5 becomes 3 and -2.5 becomes -3.
HalfEven
HalfEven = 0Round 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 = 2Round to the nearest value; a tie goes toward zero. 2.5 becomes 2 and -2.5 becomes -2.
TowardZero
TowardZero = 4Drop the digits (truncate toward zero). 2.9 becomes 2 and -2.9 becomes -2.