
Security News
TypeScript is Porting Its Compiler to Go for 10x Faster Builds
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
js-floating-point
Advanced tools
Dead-simple and lightning-fast solution for the floating-point issue in JavaScript
Floating-point numbers are represented as binary fractions. Regrettably, most decimal fractions cannot be represented exactly as binary fractions. The decimal floating-point numbers you enter are only approximated by the binary floating-point numbers actually stored in the machine. That being said, you'll see that floating-point arithmetic is NOT 100% accurate.
0.2 + 0.1
0.30000000000000004
0.3 - 0.1
0.19999999999999998
1111.11 + 1111.11 + 1111.11 + 1111.11 + 1111.11
5555.549999999999
The full article https://www.avioconsulting.com/blog/overcoming-javascript-numeric-precision-issues
All existing solutions suggest us to round the number to some predefined amount of decimal digits. Simply said, to use .toFixed(N)
. But what if I don't know how many digits do I need?
Another solution is to use the Math.js
library https://mathjs.org/docs/datatypes/bignumbers.html#roundoff-errors
Really? I have to pull a huge library to resolve so common problem?
I couldn't belive that there are no some simple solution to resolve the problem so I wrote my own dead-simple and lightning-fast solution:
const [intPart, decimalPart] = `${value}`.split('.');
const matched = decimalPart.match(/(9{6,}|0{6,})(\\d)*$/gm);
if (!matched) {
// no floating-point bug
return value;
}
const [wrongPart] = matched;
const correctDecimalsLength = decimalPart.length - wrongPart.length;
const result = parseFloat(`${intPart}.${decimalPart}`).toFixed(
correctDecimalsLength
);
To make it clear: the function is trying to find N repeating 0
s or 9
s and, if such sequence is found, it removes it rounding the last correct digit. That's all!
Just look at tests
yarn add js-floating-point
import floatingPointFix from 'js-floating-point';
0.2 + 0.1;
// WRONG: 0.30000000000000004
floatingPointFix(0.2 + 0.1);
// CORRECT: 0.3
0.3 - 0.1;
// WRONG: 0.19999999999999998
floatingPointFix(0.3 - 0.1);
// CORRECT: 0.2
FAQs
Dead-simple and lightning-fast solution for the floating-point issue in JavaScript
We found that js-floating-point demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
Research
Security News
The Socket Research Team has discovered six new malicious npm packages linked to North Korea’s Lazarus Group, designed to steal credentials and deploy backdoors.
Security News
Socket CEO Feross Aboukhadijeh discusses the open web, open source security, and how Socket tackles software supply chain attacks on The Pair Program podcast.