decimalPlaces

Returns the decimal-place count or returns a copy rounded to a specified number of decimal places.

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

const x = Arith.from("1234.56");

x.decimalPlaces(); // 2
x.decimalPlaces(1).toString(); // "1234.6"
x.decimalPlaces(2).toString(); // "1234.56"

// Negative decimalPlaces round digits to the left of the decimal point.
x.decimalPlaces(-2).toString(); // "1200"

// Pass a rounding mode to override the constructor default.
x.decimalPlaces(0, Arith.ROUND_DOWN).toString(); // "1234"
x.decimalPlaces(0, Arith.ROUND_HALF_EVEN).toString(); // "1235"

// The receiver is not mutated.
x.toString(); // "1234.56"

Arith.from("9.9e-101").decimalPlaces(); // 102

API Reference

Signature

decimalPlaces(): number | null;
decimalPlaces(decimalPlaces: number, roundingMode?: ArithRoundingMode): ArithInstance;

Parameters

ParameterTypeRequiredNotes
decimalPlacesnumberNoWhen provided, round to this many decimal places.
roundingModeArithRoundingModeNoDefaults to current ROUNDING_MODE.

Returns

In getter mode, returns a number or null for non-finite values. In rounding mode, returns a new Arith instance.

Throws

  • Throws if the numeric argument is outside its allowed range.
  • Throws if roundingMode is invalid.

Agent Contract

FieldValue
Kindinstance method
Canonical namedecimalPlaces
AliasesNone
Mutates receiverNo
Returns`number
Accepts (string, base) overloadNo
Configuration dependenciesROUNDING_MODE
Related methodsintegerValue

Agent Notes

  • decimalPlaces is the only public API for decimal-place count and decimal-place rounding; do not generate dp.
  • Disambiguate getter mode from rounding mode by checking whether the first argument is a number.
  • Import with import { Arith } from "@teakit/arith"; default imports are unsupported.
  • Use Arith.from(...) to create values. Do not generate new Arith(...) or Arith(...).
  • Use string inputs for exact decimal values, especially money-like values.
  • Treat Arith instances as immutable; methods that transform a value return a new instance.
  • Do not mutate internal fields such as c, e, s, or _isArith.