README
Javascript Number Formatter
Lightweight & Fast JavaScript Number Formatter
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.
##,###,##.#
or0#,#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'