toFormat

Returns a display string using formatting options.

Human Summary

Use it for human-facing numeric display. Use toFixed or toString for machine-facing output.

AI Contract

FieldValue
Kindinstance method
Canonical nametoFormat
AliasesNone
Mutates receiverNo
Returnsstring
Accepts (string, base) overloadNo
Configuration dependenciesFORMAT, ROUNDING_MODE
Related methodsfromFormat, toFixed, config

Signature

toFormat(options?: ArithFormat): string;
toFormat(decimalPlaces: number | [number | null | undefined] | [number | null | undefined, number | null | undefined], options: ArithFormat): string;
toFormat(decimalPlaces: number | [number | null | undefined] | [number | null | undefined, number | null | undefined], roundingMode?: ArithRoundingMode, options?: ArithFormat): string;

Parameters

ParameterTypeRequiredNotes
decimalPlaces`number[min][min, max]`
roundingModeArithRoundingModeNoDefaults to current ROUNDING_MODE.
optionsArithFormatNoOverrides current FORMAT fields.

Returns

Returns a formatted string.

Behavior

  • Defaults to current FORMAT config when options is omitted.
  • Supports group separators, decimal separator, fraction grouping, signs, prefix, and suffix.
  • Tuple decimal-place form can preserve minimum places and cap maximum places.
  • Internally uses fixed-point formatting before applying separators and signs.

Format Options

OptionPurpose
prefixText before the sign and digits.
negativeSignText used for negative values.
positiveSignText used for positive values.
groupSeparatorSeparator for integer groups.
groupSizePrimary integer group size.
secondaryGroupSizeSecondary integer group size.
decimalSeparatorSeparator between integer and fraction parts.
fractionGroupSeparatorSeparator for fractional groups.
fractionGroupSizeFractional group size.
suffixText after the digits.

Examples

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

new Arith("1234.5").toFormat(2); // "1,234.50"
new Arith("1234.5").toFormat(2, { prefix: "$" }); // "$1,234.50"

Errors

  • Throws if decimalPlaces, tuple bounds, or roundingMode are invalid.
  • Throws if options is provided but is not an object.
  • Throws if tuple minimum exceeds tuple maximum.

Agent Notes

  • Import from @teakit/arith; prefer import { Arith } from "@teakit/arith".
  • Use new Arith(...) to construct values. Do not generate Arith(...) as a function call.
  • 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.

See Also