decimal-subtract
v1.6.1
Published
Subtract decimal numbers in JavaScript without floating point errors.
Maintainers
Readme
decimal-subtract
Subtract decimal numbers in JavaScript without floating point errors.
1 - 0.55 // 0.44999999999999996
decimalSubtract(1, 0.55) // 0.45Installation
npm i decimal-subtractOr with a script tag:
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/decimal-subtract.min.js"></script>Usage
import { decimalSubtract } from 'decimal-subtract'
// or: const { decimalSubtract } = require('decimal-subtract')
decimalSubtract(1, 0.55) // 0.45
decimalSubtract(0.3, 0.1) // 0.2
decimalSubtract(123.4, 0.55) // 122.85You can pass more than one number to subtract:
decimalSubtract(10, 0.1, 0.2) // 9.7With the script tag, the function is available on window.decimalSubtract:
window.decimalSubtract.decimalSubtract(1, 0.55) // 0.45API
decimalSubtract(minuend, ...subtrahends)
Subtracts every number in subtrahends from minuend, in order, and returns
the result.
minuend(number): the number to subtract from.subtrahends(number[]): the numbers to subtract.
NaN and Infinity give the same result as the - operator.
How it works
Both numbers are multiplied by a power of 10 so they become whole numbers, the subtraction is done on those, and the result is divided back. The power of 10 comes from whichever number has more digits after the decimal point.
License
ISC
