All Projects → Mottie → Javascript Number Formatter

Mottie / Javascript Number Formatter

Licence: mit
Lightweight & Fast JavaScript Number Formatter

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Javascript Number Formatter

vue-translated
Internationalization (i18n) and localization (l10n) library for Vue.js v2.
Stars: ✭ 19 (-84.03%)
Mutual labels:  formatter, internationalization, number
Translatedjs
Internationalization and localization for JavaScript and Node.js
Stars: ✭ 17 (-85.71%)
Mutual labels:  formatter, internationalization, number
react-numeric
A react component for formatted number form fields
Stars: ✭ 30 (-74.79%)
Mutual labels:  formatter, currency, number
Easymoney
Library for operating with monetary values in JavaScript and Typescript 💵
Stars: ✭ 145 (+21.85%)
Mutual labels:  currency, formatter
Ssf
📝 Spreadsheet Number Formatter
Stars: ✭ 139 (+16.81%)
Mutual labels:  formatter, number
React Native Globalize
Internationalization (i18n) for React Native
Stars: ✭ 246 (+106.72%)
Mutual labels:  currency, internationalization
formatters
A javascript library for formatting and manipulating.
Stars: ✭ 14 (-88.24%)
Mutual labels:  formatter, currency
persian
Some utilities for Persian language in Go (Golang)
Stars: ✭ 65 (-45.38%)
Mutual labels:  currency, number
intl-format
A wrapper library for PHP to format and internationalize values in messages like sprintf
Stars: ✭ 12 (-89.92%)
Mutual labels:  formatter, internationalization
Anyformatkit
Simple text formatting in Swift
Stars: ✭ 296 (+148.74%)
Mutual labels:  currency, formatter
Go Money
Go implementation of Fowler's Money pattern
Stars: ✭ 887 (+645.38%)
Mutual labels:  currency, formatter
Moment.php
Parse, validate, manipulate, and display dates in PHP w/ i18n support. Inspired by moment.js
Stars: ✭ 900 (+656.3%)
Mutual labels:  formatter, internationalization
React Currency Formatter
💵 react component for currency formatting
Stars: ✭ 38 (-68.07%)
Mutual labels:  currency, formatter
Javamoney Lib
JavaMoney financial libraries, extending and complementing JSR 354
Stars: ✭ 104 (-12.61%)
Mutual labels:  currency
Nanocurrency Js
🔗 A toolkit for the Nano cryptocurrency, allowing you to derive keys, generate seeds, hashes, signatures, proofs of work and blocks.
Stars: ✭ 113 (-5.04%)
Mutual labels:  currency
Shiny.i18n
Shiny applications internationalisation made easy
Stars: ✭ 104 (-12.61%)
Mutual labels:  internationalization
Swimat
An Xcode formatter plug-in to format your swift code.
Stars: ✭ 1,388 (+1066.39%)
Mutual labels:  formatter
React Most Wanted
React starter kit with "Most Wanted" application features
Stars: ✭ 1,867 (+1468.91%)
Mutual labels:  internationalization
Traduora
Ever® Traduora - Open-Source Translation Management Platform
Stars: ✭ 1,580 (+1227.73%)
Mutual labels:  internationalization
Codeview
Codeview is an Android library that lets you preview code in webview very easy and simple with highlights and colors.
Stars: ✭ 103 (-13.45%)
Mutual labels:  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.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].