Skip to content

yibird/num-precision

Repository files navigation

num-precision

Perform addition, subtraction, multiplication and division operations precisely using typescript.

English · 中文

Features

  • High-precision arithmetic operations: times, divide, plus, minus
  • Floating-point correction (strip)
  • Convert float to fixed integer (float2Fixed)
  • Get number of decimal digits (getDigitLen)
  • Chainable calculation with Calc class
  • Optional JavaScript safe integer boundary checking

Install

npm install num-precision --save

# or
pnpm install num-precision --save

Methods

  • times(...nums: NumberType[]): High-precision multiplication, supports any number of arguments.
  • divide(...nums: NumberType[]): High-precision division, supports any number of arguments.
  • plus(...nums: NumberType[]): High-precision addition, supports any number of arguments.
  • minus(...nums: NumberType[]): High-precision subtraction, supports any number of arguments.
  • round(val: NumberType, decimal: number): Round a number to the specified number of decimal places.
  • strip(val: NumberType, precision = 15): Correct floating-point precision errors, keeping the specified number of significant digits.
  • float2Fixed(val: NumberType): Convert a floating-point number to an integer (fixed-point), supports scientific notation.
  • getDigitLen(val: NumberType): Get the number of decimal digits of a number, supports scientific notation.
  • toNumber(val: NumberType): Convert a string or number to a number type.
  • enableBoundaryChecking(bool = true): Enable or disable integer boundary checking, warning if the value exceeds JavaScript's safe integer range.

Usage

import {
  times,
  divide,
  plus,
  minus,
  round,
  strip,
  float2Fixed,
  getDigitLen,
  toNumber,
  enableBoundaryChecking,
  Calc,
} from "num-precision";

// -----------------------------
// High-precision arithmetic
// -----------------------------

// Multiplication
console.log(times(0.1, 0.2, 3)); // 0.06

// Division
console.log(divide(1, 3)); // 0.3333333333333333
console.log(divide(1e10, 1e5, 10)); // 1000

// Addition
console.log(plus(0.1, 0.2, 0.3)); // 0.6

// Subtraction
console.log(minus(1, 0.1, 0.2)); // 0.7

// Rounding
console.log(round(1.23456, 2)); // 1.23
console.log(round(-1.9876, 3)); // -1.988

// Floating-point correction
console.log(strip(0.10000000000000001)); // 0.1
console.log(strip(1.23456789, 5)); // 1.2346

// Float to fixed integer
console.log(float2Fixed(1.23)); // 123
console.log(float2Fixed(1.23e-2)); // 1.23

// Get decimal length
console.log(getDigitLen(0.123)); // 3
console.log(getDigitLen(1.23e-2)); // 4

// Convert to number
console.log(toNumber("123")); // 123
console.log(toNumber(456)); // 456

// Enable boundary checking
enableBoundaryChecking(true);
times(1e16, 10); // Console warning: beyond JS safe integer boundary

// -----------------------------
// Calc class (chainable operations)
// -----------------------------
const calc1 = new Calc(0.1);
const result1 = calc1
  .plus(0.2)
  .times(10)
  .minus(0.3)
  .divide(0.1)
  .round(2)
  .toNumber();
console.log(result1); // 29.0

const calc2 = new Calc(0.123456);
const result2 = calc2.strip(4).float2Fixed(0.1234).toNumber();
console.log(result2); // 1234

About

Perform addition, subtraction, multiplication and division operations precisely using typescript

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors