@algorithm.ts/calculate
TypeScript icon, indicating that this package has built-in type declarations

2.0.14 • Public • Published

@algorithm.ts/calculate


A tiny calculator for number arithmetics such as +-*/().

Install

  • npm

    npm install --save @algorithm.ts/calculate
  • yarn

    yarn add @algorithm.ts/calculate
  • deno

    import calculate from 'https://raw.githubusercontent.com/guanghechen/algorithm.ts/main/packages/calculate/src/index.ts'

Usage

// Perform integer arithmetics.
import calculate from 'algorithm.ts/calculate'
// or 
import { calculate } from 'algorithm.ts/calculate'

// Perform decimal arithmetics.
import { decimalCalculate } from 'algorithm.ts/calculate'

// Perform bigint arithmetics.
import { bigintCalculate } from 'algorithm.ts/calculate'

Examples

  • integer arithmetics

    import calculate from '@algorithm.ts/calculate'
    
    calculate('3/2')                  // => 1
    calculate('-2+1')                 // => -1
    calculate('-2*3 + 2*5*3/6')       // => -1
    calculate('(1+(4+5+2)-3)+(6+8)')  // => 23
  • decimal arithmetics

    import { decimalCalculate as calculate } from '@algorithm.ts/calculate'
    
    calculate('3/2')                  // => 1.5
    calculate('-2+1')                 // => -1
    calculate('-2*3 + 2*5*3/6')       // => -1
    calculate('(1+(4+5+2)-3)+(6+8)')  // => 23
  • bigint arithmetics

    import { bigintCalculate as calculate } from '@algorithm.ts/calculate'
    
    calculate('22222222222222222222222222222 * 3333333333333333333323232')
    // => 74074074074074074073849599999259259259259259259261504n
  • Illegal inputs

    import calculate from '@algorithm.ts/calculate'
    
    calculate('-2++1')      // => SyntaxError
    calculate('-2*/23')     // => SyntaxError
    calculate('1+(4+5+2))') // => SyntaxError
    calculate('1+(4+5+2')   // => SyntaxError
  • A solution of https://leetcode.com/problems/basic-calculator/

    export { calculate } from '@algorithm.ts/calculate'
  • A solution of https://leetcode.com/problems/basic-calculator-ii/

    export { calculate } from '@algorithm.ts/calculate'

Related

Package Sidebar

Install

npm i @algorithm.ts/calculate

Weekly Downloads

2

Version

2.0.14

License

MIT

Unpacked Size

24.3 kB

Total Files

6

Last publish

Collaborators

  • lemonclown