@algorithm.ts/calculate

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

Usage no npm install needed!

<script type="module">
  import algorithmTsCalculate from 'https://cdn.skypack.dev/@algorithm.ts/calculate';
</script>

README

@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