# a-calc

1.3.8 • Public • Published

# a-calc

A convenient library for accurate calculation and formatting of numbers, which can solve the following problems:

• Size: uncompressed size 56KB. Gzip compression size 18KB
• Make the number calculation of javascript accurate
• Other third-party libraries have poor coding experience and inconvenient formatting
• Scientific notation of possible output of numerical calculation
• Format digits, output digits in thousandths, format digits directly into percentages, keep signs of digits, output fractions directly, etc.
• Calculation or formatting of numbers with units, e.g.: 0.1% + 2%
• Calculation in scientific notation, for example: -2e3 + 6
• Support four rounding rules: rounding to the end, rounding to one, rounding to five, rounding to six (a more accurate method)

Supported operators : + - * / % **

Language： English | 简体中文

## Installation

npm install a-calc


## Import

commonjs

const {calc, fmt} = require("a-calc")
// or
const {calc, fmt} = require("a-calc/cjs") // Note that this is written to explicitly specify the use of the cjs version which makes sense, some packaging tools will do a conversion of the syntax, directly write a-calc does not work (nuxt.js is), then replace it with a-calc/cjs try

es module

import {calc, fmt} from "a-calc"
// or
const {calc, fmt} from "a-calc/es"

browser

<script src="node_modules/a-calc/browser/index.js"></script>
<script>
const {calc, fmt} = a_calc
</script>

## Usage

calc("0.1 + 0.2") // 0.3

// A little more complicated calculation
calc("0.1 + 0.2 * 0.3 / 0.4 * (0.5 + 0.6)") // 0.265

// Calculation of scientific notation
calc("-2e2 + 3e+2") // 100

Spaces are non-essential in the absence of ambiguity, and can even correctly parse the following writing calc("-2e+2+3e+2") which is difficult for the human eye to parse, but this is too low clarity, please do your best to write clearer code instead of shit! Always include spaces in your formula, which is more beautiful and clear, just like the example I wrote！！！

By the way, an ambiguous equation calc("50%%2", {_unit: true}) This ambiguity obviously occurs when calculating with units, since the parser doesn't know whether your units are % or %% so you have to use spaces to give a clear meaning, the correct way to write it would be calc("50% % 2", {_unit: true})

## Fill variables and calculate (important)

The calculated value is accurate and there is no scientific counting method

let a = 0.000001
let b = 888.789
calc("a + b", {a,b}) // 0.000001 + 888.789 = 888.789001

calc("a * (b + c) % d + 7.123", [
{a: 1, b: 2},
{c: 3, d: 4}
]) // 8.123

// A little more complicated
calc("1 + o.a / arr[0].d",{
o: { a: 2 },
arr: [{ d: 8 }]
}) // 1.25

calc("a + b - c",[
{a: 1},
{b: 2, c: 3}
])

## Calculation with unit

The reality is not always ideal, maybe we have to calculate two percentage numbers, fortunately a-calc supports these operations now, but please note that the units will be taken from the first number carrying the unit, and the subsequent units will be ignored

// It is important to note that this is required and not enabled by default, because calculations with units do some extra work, which is faster than calculations with numbers alone
calc("1 + 2%", {_unit: true}) // 3%

## Attention

• Do not wrap parentheses around a single number

## Question submission

(If you encounter any problems, please be the first to send me feedback email, 718879459@qq.com for bugs I will be the first to fix him)

## Package Sidebar

### Install

npm i a-calc

### Repository

github.com/Autumn-one/a-calc-old

38,899

1.3.8

none

203 kB

9