1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//! Rounding factory matching culori's `round.js`.
//!
//! ```js
//! const r = (value, precision) =>
//! Math.round(value * (precision = Math.pow(10, precision))) / precision;
//!
//! const round =
//! (precision = 4) =>
//! value =>
//! typeof value === 'number' ? r(value, precision) : value;
//! ```
//!
//! JavaScript's `Math.round` rounds half-toward-positive-infinity, not
//! Rust's half-away-from-zero. The two differ for exact halves of negative
//! numbers: `Math.round(-0.5) === -0`, whereas `(-0.5_f64).round() ==
//! -1.0`. This module reproduces the JS rule via `(x + 0.5).floor()`.
/// Returns a function that rounds an `f64` to the requested decimal
/// precision, matching culori's `round(precision)`. The default precision
/// in culori is `4`; callers pass the value explicitly here.
///
/// ```rust
/// let r = culors::round(2);
/// assert_eq!(r(0.123), 0.12);
/// assert_eq!(r(1.235), 1.24);
/// ```