typst_utils

Function round_with_precision

source
pub fn round_with_precision(value: f64, precision: i16) -> f64
Expand description

Returns value with n digits after floating point where n is precision. Standard rounding rules apply (if n+1th digit >= 5, round away from zero).

If precision is negative, returns value with n less significant integer digits before floating point where n is -precision. Standard rounding rules apply to the first remaining significant digit (if nth digit from the floating point >= 5, round away from zero).

If rounding the value will have no effect (e.g., it’s infinite or NaN), returns value unchanged.

Note that rounding with negative precision may return plus or minus infinity if the result would overflow or underflow (respectively) the range of floating-point numbers.

§Examples

let rounded = round_with_precision(-0.56553, 2);
assert_eq!(-0.57, rounded);

let rounded_negative = round_with_precision(823543.0, -3);
assert_eq!(824000.0, rounded_negative);