• Stars
    star
    140
  • Rank 261,473 (Top 6 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 10 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Lightweight & Fast JavaScript Number Formatter

Javascript Number Formatter

Lightweight & Fast JavaScript Number Formatter

Build Status NPM Version devDependency Status MIT

Introduction

This standalone number formatter† is intended to be short and fast. As they are the main factors for a high performance JavaScript app. Development release is around 150 lines including license info, blank lines and comments. And production release is less than 2,000 bytes.

format( "#,##0.####", 1234567.890 );  // output: "1,234,567.89"
format( "$ #,###.00", -1234567.890 ); // output: "$ -1,234,567.89"

// Added in v2.0.0
format( "$ #,###.00", -1234567.890, {enforceMaskSign: true});  // output: "$ 1,234,567.89"
format( "$ -#,###.00", -1234567.890, {enforceMaskSign: true}); // output: "$ -1,234,567.89"
format( "$ +#,###.00", -1234567.890, {enforceMaskSign: true}); // output: "$ -1,234,567.89"
format( "$ +#,###.00", 1234567.890, {enforceMaskSign: true});  // output: "$ +1,234,567.89"

† Initial development release of this code was written by KPL and hosted at Google Code.

Features

  • Short, fast, flexible yet standalone.
  • Accept standard number formatting like #,##0.00 or with negation -000.####.
  • Accept any country format like # ##0,00, #,###.##, #'###.## or any type of non-numbering symbol.
  • Accept any numbers of digit grouping. #,##,#0.000 or #,###0.## are all valid.
  • Accept any redundant/fool-proof formatting. ##,###,##.# or 0#,#00#.###0# are all OK.
  • Auto number rounding.
  • Simple interface, just supply mask & value like this: format( "0.0000", 3.141592).
  • Include a prefix & suffix with the mask.

Limitations

  • No scientific/engineering formatting.
  • Not for date or phone formation.
  • No color control.
  • No prefix or suffix is allowed except leading negation symbol. So $#,##0.00 or #,###.##USD will not yield expected outcome. Use '$'+format('#,##0.00', 123.45) or format('#,##0.00', 456.789) + 'USD'
  • The prefix or suffix can not include any numbers (0-9), dashes (-), or plus signs (+).

Format Symbols

Description Symbol Summary
Mask symbols #0123456789+- Mask symbols used for formatting the value.
Placeholders #123456789 Un-forced digit*; this optional digit will only show if it is required as a placeholder.
Zero 0 Forced digit; the digit will be shown whether or not the digit is relevant to the value.
Signs +- Indicates a positive or negative value; visible depending on the value sign and the enforceMaskSign setting.
Leftmost Any non-mask symbol† inside the mask will be set to represent a thousands separator.
Rightmost Any non-mask symbol† inside the mask‡ will be set to represent the decimal separator.
Prefix/Suffix Any non-mask symbol† outside the mask.

* Non-zero mask digits (1 through 9) behave the same as the #.
† Anything not a digit, and not a +, - or #.
‡ In the case where there is a trailing decimal or comma, it will be included in the mask, e.g. #. or 0,.

Note

When only one symbol is supplied, the library will always treat that symbol as a decimal. For example, format( '#,###', 1234567.890) will output 1234567,890.

To force a single symbol to be used as a separator, add a trailing symbol. In this example, a period is added to the end of the mask - format( '#,###.', 1234567.890) - resulting in it being used as a decimal and forcing the first symbol to be the separator and return this output: 1,234,567.

Installation

npm package

npm install --save number-format.js

Demos

A demo/sample page with few examples is provided (demo).

And a jsFiddle was created to aid in testing: https://jsfiddle.net/Mottie/t2etyodx/

Recent Changes

View the complete change log here.

v2.0.7 (2018-11-13)

  • Update typescript binding. See issue #20.
  • Fix improper placeholder behavior. Updated Readme with format symbols table. Closes issue #19.
  • Add more tests.
  • Meta:
    • Update dependencies.
    • Improve code readability.
    • Include version in min.js.

v2.0.6 (2018-11-06)

  • Trim trailing zeros in mask. Fixes issue #18.

v2.0.0 – 2.0.5 (2018-10-26)

  • Add ignoreSign option (modified to enforeceMaskSign!).
  • Switch to XO, AVA & rollup.
  • Meta: Update dot files & remove bower support.
  • Code cleanup & convert to ES2015.
    • Rename ignoreSign to enforceMaskSign (default false).
    • Reduce code complexity.
    • Export as node module.
    • Update TS with options.
    • Switch demo to use lib file & highlight valid results.
  • Switch from Grunt to rollup.
  • Switch from IIFE to UMD output.

More Repositories

1

tablesorter

Github fork of Christian Bach's tablesorter plugin + awesomeness ~
JavaScript
2,605
star
2

GitHub-userscripts

Userscripts to add functionality to GitHub
JavaScript
1,782
star
3

Keyboard

Virtual Keyboard using jQuery ~
JavaScript
1,773
star
4

Darker-Medium

Read Medium stories with happy eyes!
JavaScript
128
star
5

input-password-bullet

How to replace the input password bullet in most browsers
CSS
87
star
6

visualNav

A jQuery plugin that modifies a navigation menu to highlight / change when the menu's target smooth scrolls into view
HTML
68
star
7

Octopatcher

Arrgh Some Patchy Goodness to GitHub!
JavaScript
59
star
8

Kwicks

â›” Kwicks for jQuery - fork of jQuery Kwicks by Jeremy Martin
JavaScript
49
star
9

github-reserved-names

Get a list, or check if a user or organization name is reserved by GitHub
JavaScript
46
star
10

Misc-userscripts

Userscripts to add functionality to miscellaneous sites
JavaScript
30
star
11

Pointy

A jQuery plugin that dynamically points one element at another ~
JavaScript
26
star
12

Keycaster

A jQuery plugin that adds a keystroke and mouse visualizer to your browser window.
JavaScript
16
star
13

Pathslider

Numerical slider that follows a Bezier path
JavaScript
16
star
14

Jatt

Just another Tooltip
HTML
7
star
15

Unicode

HTML Escape Codes for UTF-8 (0-65,535)
JavaScript
6
star
16

ZenHub-userscripts

â›” BROKEN! Userscripts to add functionality to ZenHub
JavaScript
6
star
17

imageHighlighter

A jQuery plugin that highlight blocks of an image when hovering over a link.
JavaScript
6
star
18

css3-background-utilities

Prepend, append & remove multiple background images
JavaScript
5
star
19

FancySelector

Select replacement, but fancy!
JavaScript
5
star
20

imagesLoaded

Just a little script to run a callback when all images have completed loading; it ignores image load errors.
JavaScript
5
star
21

Presentations

Presentations for lightning talks
JavaScript
4
star
22

squeezeBox

SqueezeBox adds a whole page sliding accordion effect to your content
JavaScript
4
star
23

color-bundle

Rollup bundled version of Qix-/color
JavaScript
3
star
24

Vertical-Parallax

Vertical Parallax Demo
3
star
25

WebSearch

A jQuery plugin to add a search button to your site
JavaScript
3
star
26

progressIndicator

A jQuery plugin that allows manual control of a progress or loading indicator
JavaScript
3
star
27

join-non-empty-array

Join non-empty array elements into a string
JavaScript
3
star
28

is-regexp-string

Check if a string is a regular expression
JavaScript
3
star
29

todaysImage

A jQuery plugin that will take a group of images with a set date range, and show a random image based on the current date.
JavaScript
3
star
30

WebSearchUI

A jQuery UI Widget to add a search button to your site.
JavaScript
2
star
31

va-page-checker

Highlight web components, accessibility issues, and other issues on VA.gov pages
JavaScript
2
star