round
$round
Rounds a numeric value to the nearest integer or to a specified number of decimal places. Safe to use in normalize phase (non-throwing). Non-numeric values pass through unchanged.
Parameters
precision(number, optional): Number of decimal places. Defaults to 0 (integer rounding).
Examples:
3.7with precision 0 →43.14159with precision 2 →3.14123.456with precision 1 →123.5"not a number"→"not a number"(passes through unchanged)
Example
// Round to nearest integer
new Schema('number').transformer('$round')
// 3.5 → 4, 3.4 → 3
// Round a price to 2 decimal places
new Schema('number').transformer({$round: {precision: 2}})
// 9.9950 → 9.99, 9.9951 → 10.00
// Round a sensor reading to 1 decimal place
new Schema('object', {
temperature: new Schema('number').transformer({$round: {precision: 1}}),
})