toNearest

Rounds the receiver to the nearest multiple of a supplied step value.

import { Arith } from "@teakit/arith";

Arith.from("1.08").toNearest("0.05").toString(); // "1.1"
Arith.from("1.08").toNearest("0.05", Arith.ROUND_DOWN).toString(); // "1.05"
Arith.from("123").toNearest("10").toString(); // "120"

// Use it for business increments such as price ticks or lot sizes.
Arith.from("7.49").toNearest("0.25").toString(); // "7.5"
Arith.from("7.49").toNearest("0.25", Arith.ROUND_FLOOR).toString(); // "7.25"

API Reference

Signature

toNearest(step: ArithValue, roundingMode?: ArithRoundingMode): ArithInstance;

Parameters

ParameterTypeRequiredNotes
stepArithValueYesPositive finite step size.
roundingModeArithRoundingModeNoDefaults to current ROUNDING_MODE.

Returns

Returns a new Arith instance rounded to a multiple of step.

Throws

  • Throws if step is zero, negative, NaN, or infinite.
  • Throws if roundingMode is invalid.
  • Throws if an input value is invalid while STRICT is true.

Agent Contract

FieldValue
Kindinstance method
Canonical nametoNearest
AliasesNone
Mutates receiverNo
ReturnsArithInstance
Accepts (string, base) overloadNo
Configuration dependenciesROUNDING_MODE
Related methodsround, decimalPlaces

Agent Notes

  • Use string inputs for exact step sizes.
  • Use decimalPlaces(decimalPlaces, roundingMode?) when the rule is decimal places; use toNearest() for ticks, lot sizes, or other step multiples.
  • Treat Arith instances as immutable.