Security News
Bun 1.2 Released with 90% Node.js Compatibility and Built-in S3 Object Support
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
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 4 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.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
Security News
Biden's executive order pushes for AI-driven cybersecurity, software supply chain transparency, and stronger protections for federal and open source systems.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.