Git Product home page Git Product logo

javascript-number-formatter's Introduction

Javascript Number Formatter

Lightweight & Fast JavaScript Number Formatter

Build Status

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 as short as < 120 lines including license info, blank lines and comments. And production release is less than 1,200 bytes.

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

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 (+).

Installation

npm package

npm install --save number-format.js

bower

bower install number-format.js --save

Note

When there's only one symbol is supplied, system will always treat the single symbol as Decimal. For instance, format( '#,###', 1234567.890) will output 1234567,890. To force a single symbol as Separator, add a trailing dot to the end like this: format( '#,###.', 1234567.890) which will then output 1,234,567.

A demo/sample page with few examples is provided (DEMO). The code is safe to be minimized using Google Compiler in Advanced mode.

Version

V1.1.5 (1/26/2015)

  • Change name in bower.json & package.json to "number-format.js".
  • Update readme with installation instructions.

v1.1.4 (1/26/2015)

  • Add AMD & NodeJS compatibility (UMD). Thanks MadMG!
  • Renamed variables & code cleanup to make it more readable.

v1.1.3 (11/12/2014)

  • Fix bower link to point to the production version. Thanks marcelboettcher!
  • Minor code & demo cleanup.

v1.1.2 (6/22/2014)

  • Remove negative sign from results with a value of zero.
  • Added more examples to demo page.

javascript-number-formatter's People

Contributors

madmg avatar marcelboettcher avatar mottie avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.