toPrecision

Formats the receiver as a string rounded to significant digits.

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

const x = Arith.from("45.6");
const y = Arith.from("45.6");

x.toPrecision(); // "45.6"
x.toPrecision(1); // "5e+1"
y.toPrecision(1); // "5e+1"
y.toPrecision(2, Arith.ROUND_UP); // "46"
y.toPrecision(2, Arith.ROUND_DOWN); // "45"
x.toPrecision(5); // "45.600"
y.toPrecision(5); // "45.600"

API Reference

Signature

toPrecision(significantDigits?: number, roundingMode?: ArithRoundingMode): string;

Parameters

ParameterTypeRequiredNotes
significantDigitsnumberNoSignificant digits to include.
roundingModeArithRoundingModeNoDefaults to current ROUNDING_MODE.

Returns

Returns a string.

Throws

  • Throws if significantDigits or roundingMode is invalid.

Agent Contract

FieldValue
Kindinstance method
Canonical nametoPrecision
AliasesNone
Mutates receiverNo
Returnsstring
Accepts (string, base) overloadNo
Configuration dependenciesROUNDING_MODE
Related methodsprecision, toFixed, toExponential

Agent Notes

  • 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.