Methods for converting fractions to various string and numeric representations, including decimal, fractional, LaTeX, and continued fraction formats.
Converts the fraction to a JavaScript number (may lose precision for large fractions).
/**
* Converts this fraction to a JavaScript number
* @returns Numeric approximation of the fraction (may lose precision)
*/
function valueOf(): number;Usage Examples:
import Fraction from "fraction.js";
// Basic conversion
const f1 = new Fraction(3, 4);
const num1 = f1.valueOf(); // 0.75
// Repeating decimal
const f2 = new Fraction(1, 3);
const num2 = f2.valueOf(); // 0.3333333333333333
// Large numbers (may lose precision)
const big = new Fraction("123456789012345678901234567890");
const numBig = big.valueOf(); // May be Infinity or lose precision
// Use in numeric operations
const f3 = new Fraction(7, 8);
const result = Math.sin(f3.valueOf()); // 0.8206...Converts the fraction to a decimal string representation, optionally showing repeating decimal patterns.
/**
* Converts this fraction to a decimal string representation
* @param dec - Maximum decimal places to show (default: 15)
* @returns String representation with repeating decimals in parentheses
*/
function toString(dec?: number): string;Usage Examples:
import Fraction from "fraction.js";
// Basic decimal representation
const f1 = new Fraction(3, 4);
console.log(f1.toString()); // "0.75"
// Repeating decimals
const f2 = new Fraction(1, 3);
console.log(f2.toString()); // "0.(3)"
const f3 = new Fraction(22, 7);
console.log(f3.toString()); // "3.(142857)"
// Mixed repeating
const f4 = new Fraction(5, 6);
console.log(f4.toString()); // "0.8(3)"
// Control decimal places
const f5 = new Fraction(1, 7);
console.log(f5.toString(3)); // "0.142" (limited to 3 places)
console.log(f5.toString(10)); // "0.(142857)" (shows full pattern)
// Integer fractions
const f6 = new Fraction(8, 2);
console.log(f6.toString()); // "4"
// Negative fractions
const f7 = new Fraction(-7, 12);
console.log(f7.toString()); // "-0.58(3)"Converts the fraction to a string in fraction notation (numerator/denominator).
/**
* Converts this fraction to fraction notation string
* @param showMixed - If true, shows mixed numbers (e.g., "1 1/4" instead of "5/4")
* @returns String in fraction notation
*/
function toFraction(showMixed?: boolean): string;Usage Examples:
import Fraction from "fraction.js";
// Basic fraction notation
const f1 = new Fraction(3, 4);
console.log(f1.toFraction()); // "3/4"
// Improper fractions
const f2 = new Fraction(7, 4);
console.log(f2.toFraction()); // "7/4"
console.log(f2.toFraction(true)); // "1 3/4" (mixed number)
// Integer values
const f3 = new Fraction(8, 2);
console.log(f3.toFraction()); // "4"
// Negative fractions
const f4 = new Fraction(-5, 8);
console.log(f4.toFraction()); // "-5/8"
console.log(f4.toFraction(true)); // "-5/8" (can't show mixed for proper fraction)
// Mixed number examples
const f5 = new Fraction(11, 3);
console.log(f5.toFraction()); // "11/3"
console.log(f5.toFraction(true)); // "3 2/3"
const f6 = new Fraction(-13, 4);
console.log(f6.toFraction(true)); // "-3 1/4"Converts the fraction to LaTeX mathematical notation for typesetting.
/**
* Converts this fraction to LaTeX notation
* @param showMixed - If true, shows mixed numbers in LaTeX format
* @returns String in LaTeX fraction notation
*/
function toLatex(showMixed?: boolean): string;Usage Examples:
import Fraction from "fraction.js";
// Basic LaTeX fractions
const f1 = new Fraction(3, 4);
console.log(f1.toLatex()); // "\\frac{3}{4}"
// Mixed numbers in LaTeX
const f2 = new Fraction(7, 4);
console.log(f2.toLatex()); // "\\frac{7}{4}"
console.log(f2.toLatex(true)); // "1\\frac{3}{4}"
// Integer values
const f3 = new Fraction(5);
console.log(f3.toLatex()); // "5"
// Negative fractions
const f4 = new Fraction(-2, 3);
console.log(f4.toLatex()); // "-\\frac{2}{3}"
// Use in LaTeX documents
const latex = `The result is $${f1.toLatex()}$ of the total.`;
// "The result is $\\frac{3}{4}$ of the total."Converts the fraction to its continued fraction representation.
/**
* Converts this fraction to continued fraction representation
* @returns Array of BigInt values representing the continued fraction
*/
function toContinued(): bigint[];Usage Examples:
import Fraction from "fraction.js";
// Simple fractions
const f1 = new Fraction(3, 4);
console.log(f1.toContinued()); // [0n, 1n, 3n] represents 0 + 1/(1 + 1/3)
const f2 = new Fraction(7, 3);
console.log(f2.toContinued()); // [2n, 3n] represents 2 + 1/3
// Golden ratio approximation
const f3 = new Fraction(8, 5); // Fibonacci ratio
console.log(f3.toContinued()); // [1n, 1n, 1n, 1n, 1n] all ones!
// Pi approximation (22/7)
const pi = new Fraction(22, 7);
console.log(pi.toContinued()); // [3n, 7n] represents 3 + 1/7
// Use continued fraction for analysis
const sqrt2Approx = new Fraction(99, 70); // Approximates √2
const cf = sqrt2Approx.toContinued();
console.log(cf); // Shows convergent patternCreates an exact copy of the fraction.
/**
* Creates an exact copy of this fraction
* @returns New Fraction instance with identical value
*/
function clone(): Fraction;Usage Examples:
import Fraction from "fraction.js";
// Basic cloning
const original = new Fraction(5, 7);
const copy = original.clone();
console.log(copy.equals(original)); // true
// Independent modification
const f1 = new Fraction(1, 2);
const f2 = f1.clone();
const modified = f1.add(1, 4); // f1 is immutable, so this creates new instance
console.log(f2.equals(1, 2)); // true (f2 unchanged)
// Use for backup before operations
const important = new Fraction("0.(142857)");
const backup = important.clone();
// ... perform operations on important ...
// backup retains original valueSimplifies the fraction using continued fraction approximation within a tolerance.
/**
* Simplifies this fraction using continued fraction approximation
* @param eps - Tolerance for approximation (default: 1e-3)
* @returns New simplified Fraction within the given tolerance
*/
function simplify(eps?: number): Fraction;Usage Examples:
import Fraction from "fraction.js";
// Simplify complex fraction
const complex = new Fraction(1000001, 1000003);
const simple = complex.simplify();
console.log(simple.toString()); // Much simpler approximation
// Control tolerance
const f1 = new Fraction(355, 113); // Good pi approximation
const loose = f1.simplify(0.1); // Looser tolerance
const tight = f1.simplify(0.0001); // Tighter tolerance
// Simplify decimal approximations
const decimal = new Fraction(0.333333);
const simplified = decimal.simplify(0.001);
console.log(simplified.toFraction()); // Likely "1/3"
// Already simple fractions
const simple = new Fraction(1, 2);
const stillSimple = simple.simplify();
console.log(stillSimple.equals(simple)); // true (no change needed)Different output formats for the same fraction:
import Fraction from "fraction.js";
const f = new Fraction(22, 7); // Pi approximation
console.log("valueOf():", f.valueOf()); // 3.142857142857143
console.log("toString():", f.toString()); // "3.(142857)"
console.log("toFraction():", f.toFraction()); // "22/7"
console.log("toFraction(true):", f.toFraction(true)); // "3 1/7"
console.log("toLatex():", f.toLatex()); // "\\frac{22}{7}"
console.log("toLatex(true):", f.toLatex(true)); // "3\\frac{1}{7}"
console.log("toContinued():", f.toContinued()); // [3n, 7n]
// Negative example
const neg = new Fraction(-17, 5);
console.log("valueOf():", neg.valueOf()); // -3.4
console.log("toString():", neg.toString()); // "-3.4"
console.log("toFraction():", neg.toFraction()); // "-17/5"
console.log("toFraction(true):", neg.toFraction(true)); // "-3 2/5"
console.log("toLatex():", neg.toLatex()); // "-\\frac{17}{5}"
console.log("toLatex(true):", neg.toLatex(true)); // "-3\\frac{2}{5}"
console.log("toContinued():", neg.toContinued()); // [-4n, 1n, 1n, 2n]The toString() method automatically detects and displays repeating decimal patterns:
import Fraction from "fraction.js";
// Simple repeating patterns
console.log(new Fraction(1, 3).toString()); // "0.(3)"
console.log(new Fraction(2, 3).toString()); // "0.(6)"
console.log(new Fraction(1, 9).toString()); // "0.(1)"
console.log(new Fraction(1, 11).toString()); // "0.(09)"
// Complex repeating patterns
console.log(new Fraction(1, 7).toString()); // "0.(142857)"
console.log(new Fraction(1, 13).toString()); // "0.(076923)"
// Mixed non-repeating and repeating
console.log(new Fraction(1, 6).toString()); // "0.1(6)"
console.log(new Fraction(5, 12).toString()); // "0.41(6)"
// Long patterns are truncated
console.log(new Fraction(1, 97).toString()); // Shows pattern up to limitimport Fraction from "fraction.js";
// valueOf() may lose precision
const precise = new Fraction("0.1").add("0.2");
console.log(precise.toString()); // "0.3" (exact)
console.log(precise.valueOf()); // 0.3 (exact in this case)
// Large numbers
const huge = new Fraction("123456789012345678901234567890");
console.log(huge.toString()); // Full precision string
console.log(huge.valueOf()); // May be Infinity
// Use string methods for exact output
const calculation = new Fraction(1, 3).mul(7).add(1, 21);
console.log("Exact:", calculation.toString()); // Exact decimal
console.log("Fraction:", calculation.toFraction()); // Exact fraction
console.log("Approximate:", calculation.valueOf()); // Approximate number