Function round
pub fn round(value: DecNum, digits: i64) -> Result<DecNum, EcoString>
Expand description
Rounds a number to the nearest integer away from zero.
Optionally, a number of decimal places can be specified.
If the number of digits is negative, its absolute value will indicate the amount of significant integer digits to remove before the decimal point.
Note that this function will return the same type as the operand. That is,
applying round
to a [float
] will return a float
, and to a [decimal
],
another decimal
. You may explicitly convert the output of this function to
an integer with [int
], but note that such a conversion will error if the
float
or decimal
is larger than the maximum 64-bit signed integer or
smaller than the minimum integer.
In addition, this function can error if there is an attempt to round beyond
the maximum or minimum integer or decimal
. If the number is a float
,
such an attempt will cause {float.inf}
or {-float.inf}
to be returned
for maximum and minimum respectively.
#calc.round(3.1415, digits: 2)
#assert(calc.round(3) == 3)
#assert(calc.round(3.14) == 3)
#assert(calc.round(3.5) == 4.0)
#assert(calc.round(3333.45, digits: -2) == 3300.0)
#assert(calc.round(-48953.45, digits: -3) == -49000.0)
#assert(calc.round(3333, digits: -2) == 3300)
#assert(calc.round(-48953, digits: -3) == -49000)
#assert(calc.round(decimal("-6.5")) == decimal("-7"))
#assert(calc.round(decimal("7.123456789"), digits: 6) == decimal("7.123457"))
#assert(calc.round(decimal("3333.45"), digits: -2) == decimal("3300"))
#assert(calc.round(decimal("-48953.45"), digits: -3) == decimal("-49000"))