Skip to content

A function which fixes floating-point rounding errors in JavaScript

License

Notifications You must be signed in to change notification settings

tetrogem/fix-floats

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

fix-floats

A function which fixes floating-point rounding errors in JavaScript

Importing fixFloat:

import fixFloat from "fix-floats";

Fix floating-point errors to specified number of decimal places:

const num = 0.101 + 0.202; // 0.30300000000000005

// to significant decimal places:
fixFloat(num, 3) // 0.303

// with less decimal places:
fixFloat(num, 2) // 0.3
fixFloat(num, 1) // 0.3

// with more decimal places:
fixFloat(num, 4) // 0.303
fixFloat(num, 5) // 0.303

Fix floating-point errors with predicted number of wanted decimal places:

const num = 0.101 + 0.202; // 0.30300000000000005

// decimal place prediction:
fixFloat(num) // 0.303

About

A function which fixes floating-point rounding errors in JavaScript

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published