npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

decifloat

v0.1.0

Published

Format floats as decimals as accurately as possible.

Downloads

32

Readme

decifloat

Build Status Coverage Status npm version

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.

Installation

npm install --save decifloat

Usage

decifloat.toFixed(num, minDecimals, maxDecimals, roundingFunc = Math.round)

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

Explanation

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.