Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
decifloat
Advanced tools
Readme
Format floats as decimals with expected rounding
This module formats floats to a given number of decimals with expected rounding, fixing some surprises with native Javascript method toFixed. It seeks to do this with minimal overhead.
npm install --save decifloat
Formats num
to at least minDecimals
and at most maxDecimals
decimal digits after the decimal point.
import {toFixed} from 'decifloat';
console.log(toFixed(1.005, 0, 2)); // Outputs 1.01
console.log(toFixed(1.005, 0, 5)); // Outputs 1.005
console.log(toFixed(1.005, 5, 5)); // Outputs 1.00500
console.log(toFixed(1.005, 0, 1)); // Outputs 1
Floats in Javascript (as in most modern languages) are represented using powers of 2, which means that most decimal fractions are not represented precisely. For example, 1.005 is not representable precisely in Javascript, and is represented by a slightly smaller number.
The native methods of Number
such as toString()
and toExponential()
do a good job of formatting this slightly smaller number back as "1.005"
for output.
However, because Javascript's 1.005 is actually a slightly smaller than the mathematical 1.005, rounding to two
decimal digits, as done by (1.005).toFixed(2)
, produces "1.00"
, which looks wrong. For the
same reason, 1.005 * 100
produces 100.49999999999999
.
This module solves this issue by scaling "1.005"
to "100.5"
as a string before rounding. Because
it doesn't lose precision in scaling by powers of 10, decifloat.toFixed(1.005, 2, 2)
can
correctly produce "1.01"
.
Beyond that, it aims to be fast. In many cases (at least on node 10) it's actually faster than
the native toFixed()
method.
FAQs
Format floats as decimals as accurately as possible.
The npm package decifloat receives a total of 2 weekly downloads. As such, decifloat popularity was classified as not popular.
We found that decifloat 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.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.