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

numberparser

v0.0.6

Published

A simple number formatting utility for Javascript and Node.js

Downloads

5

Readme

Build Status Coverage Status

NumberParser

A simple number formatting utility for Javascript and Node.js

Installation

Installation via NPM

$ npm install numberparser

Installation via Bower

$ bower install numberparser

Including the library

If you are using Node.js you can do the following..

var NumberParser = require('numberparser');

If you are using the browser you can include the Javascript file and NumberParser will be exported to the window object.

<script src="/bower_components/numberparser/src/numberparser.min.js" type="text/javascript"></script>

Using the library

Format
% [+-] [width] [[.] [precision]] [s] [df]
  • +- If set + indicates to pad the number on the right. If set to - the number will be padding on the left. Defaults to +
  • width The width of the number. If a width is specified the number will be scaled to match the input width (applying padding if required). When padding is applied the first digit of width is used to determine the padding character and the following digits represent the width. Defaults to 12
  • precision if a decimal point is given in the value and a precision is specified then the remainder on the number will be rounded or padded to the proper precision. Defaults to 2
  • s This flag indicates whether or not you want separators to appear in the formatted value
  • d|f The output type, use d for integer values and f for floating point values
Examples
var parser = new NumberParser();

// Format decimal numbers
assert.equal('123',        parser.parseValue(123, "%d"));
assert.equal('0045',       parser.parseValue(45, "%-04d"));

// Format floating point numbers
assert.equal('123.46',     parser.parseValue(123.4567, "%.2f"));
assert.equal('45.000',     parser.parseValue(45, "%.3f"));

// Format a currency value (symbols can be freely appended or prepended to the format)
assert.equal('$123.46',    parser.parseValue(123.456, "$%.2f"));

// Format a percentage value
assert.equal('45.20%',     parser.parseValue(45.2, "%.2f%"));

// Add thousands separators using the "s" flag
assert.equal('10,000',     parser.parseValue(10000, "%sd"));
assert.equal('2,345.67',   parser.parseValue(2345.67, "%sf"));

// You can also customize the formatting by passing in an options object
var customParser = new NumberParser({ separator: '.', decimalPoint: ',' });
assert.equal('€10.500,25', customParser.parseValue(10500.25, "€%sf"));

Compiling the source code

If you want to recompile the source files you will need to use the Google Closure Compiler. After you have it installed execute the following commands below.

$ export CLOSURE_COMPILER_PATH=/path/to/closure-compiler.jar

$ java -jar $CLOSURE_COMPILER_PATH \
    --create_source_map=numberparser.map \
    --compilation_level=ADVANCED \
    --warning_level=DEFAULT \
    --js_output_file=numberparser.min.js \
    numberparser.js