Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/stutrek/fix-a-float
99.0000000000000005% of the time floating point math is perfect
https://github.com/stutrek/fix-a-float
Last synced: about 1 month ago
JSON representation
99.0000000000000005% of the time floating point math is perfect
- Host: GitHub
- URL: https://github.com/stutrek/fix-a-float
- Owner: stutrek
- Created: 2015-10-28T18:30:50.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2017-08-10T01:13:47.000Z (over 7 years ago)
- Last Synced: 2024-10-11T13:23:00.039Z (about 1 month ago)
- Language: JavaScript
- Homepage:
- Size: 7.81 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# ![Fix-A-Float](http://stutrek.github.io/fix-a-float/fix-a-float.svg)
99.0000000000000005% of the time floating point math is perfect. The rest of the time we have this problem:
```
>> 0.1 + 0.2
0.30000000000000004
```This solves that problem.
```
>> fixAfloat(0.1 + 0.2)
0.3
```## Installation
`npm install fix-a-float`
## Usage
```
var fixAfloat = require('fix-a-float');console.log(fixAfloat(0.1+0.2));
```## How does it do it?
It converts the number to a string, looks for a long run of zeros or nines at the end, removes them, and coverts back to a float.
### Should I use this all the time, everywhere?
Absolutely not. Only use it in places where you need human readable numbers but don't know the precision you need. It doesn't actually solve any of the larger issues with floats.