toFraction

Returns a simple fraction representing the value.

Summary

Use it when decimal values need to be shown or stored as rational numbers.

AI Contract

FieldValue
Kindinstance method
Canonical nametoFraction
AliasesNone
Mutates receiverNo
Returns[ArithInstance, ArithInstance]
Accepts (string, base) overloadNo
Configuration dependenciesSTRICT
Related methodstoString, toObject

Signature

toFraction(maxDenominator?: ArithValue): [ArithInstance, ArithInstance];

Parameters

ParameterTypeRequiredNotes
maxDenominatorArithValueNoPositive integer denominator limit, or Infinity.

Returns

Returns [numerator, denominator], both as Arith instances.

Behavior

  • Without a maximum denominator, returns the exact fraction with the smallest necessary denominator.
  • With a maximum denominator, returns the closest fraction whose denominator does not exceed the limit.
  • For non-finite values, returns a non-finite numerator and denominator zero.

Examples

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

const [n, d] = new Arith("1.75").toFraction();

n.toString(); // "7"
d.toString(); // "4"

Errors

  • Throws if maxDenominator is not a positive integer or Infinity.
  • Throws if maxDenominator is invalid while STRICT is true.

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