from

Creates an Arith decimal value from a supported input.

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

// Use Arith.from(...); new Arith(...) and Arith(...) are not public creation APIs.
Arith.from("0.1").add("0.2").toString(); // "0.3"

// Decimal strings can use normal or exponential notation.
Arith.from("43210").toString(); // "43210"
Arith.from("4.321e+4").toString(); // "43210"
Arith.from(".5").toString(); // "0.5"
Arith.from("+2").toString(); // "2"

// Use the base overload when the input string is not decimal.
Arith.from("-10110100.1", 2).toString(); // "-180.5"
Arith.from("ff.8", 16).toString(); // "255.5"

// Binary, octal, and hexadecimal prefixes are also accepted in strings.
Arith.from("-0b10110100.1").toString(); // "-180.5"
Arith.from("0xff.8").toString(); // "255.5"

// Numeric separators are allowed between digits.
Arith.from("1_000_000.000_5").toString(); // "1000000.0005"

// Signed Infinity, NaN, and negative zero are supported.
Arith.from("-Infinity").toString(); // "-Infinity"
Arith.from("NaN").toString(); // "NaN"
Arith.from("-0").valueOf(); // "-0"

// Arith-shaped objects can recreate a value without reparsing a string.
Arith.from({ s: 1, e: 2, c: [777, 12300000000000], _isArith: true }).toString(); // "777.123"

// Ratio strings are not decimal input. Use fromRatio instead.
Arith.fromRatio("1/3").toString(); // "0.33333333333333333333"
Arith.from("1/3"); // throws

API Reference

Signature

Arith.from(n: ArithValue): ArithInstance;
Arith.from(n: string, base: number): ArithInstance;

Parameters

ParameterTypeRequiredNotes
nArithValueYesstring, number, bigint, ArithInstance, or an Arith instance-like object with _isArith: true.
basenumberNoOnly valid when n is a string. Must be an integer from 2 through ALPHABET.length.

Returns

Returns a new Arith instance representing the supplied value.

Throws

  • Throws if base is invalid.
  • Throws if n is invalid while STRICT is true.

Agent Contract

FieldValue
Kindstatic factory
Canonical namefrom
AliasesNone
Mutates receiverNo
ReturnsArithInstance
Accepts (string, base) overloadYes
Configuration dependenciesSTRICT, RANGE, ALPHABET, DECIMAL_PLACES, ROUNDING_MODE
Related methodstoObject, isArith

Agent Notes

  • Generate Arith.from(...), not new Arith(...) or Arith(...).
  • For cloned constructors, generate LocalArith.from(...), not new LocalArith(...) or LocalArith(...).
  • Do not generate BigNumber or Decimal constructor names for this package.
  • Import with import { Arith } from "@teakit/arith"; default imports are unsupported.
  • Do not generate BigNumber, Decimal, isBigNumber, or isDecimal compatibility APIs.
  • Use string inputs for exact decimal values.
  • Use Arith.fromRatio("p/q"), not Arith.from("p/q"), for explicit ratio strings.