Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
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
Mainly used to implement toFixed()
, this parses a float into a significant and a decimal
exponent, and maintains that representation internally. It allows scaling by a power of 10,
determining the number of significant digits after the decimal point, and rounding to a given
number of decimals. For details, see the documentation comments in lib/index.ts
.
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 tries to minimize string operations, and so achieves performance equivalent to
about 3 or 4 calls of the native toFixed()
method.
FAQs
Format floats as decimals as accurately as possible.
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
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.