JavaScript Number Formatter
Test/Sample Page

Usage

// format( mask, value );
// result "1,234,567.89"
format( "#,##0.####", 1234567.890 );

Examples

#DescriptionInputFormatOutput
Most common world wide
Simple 123456.789 #,##0.00
Random 20110628.15001234 #,##0.###0
Random 0 #,###.##0
Long number 1234567890.1234567890 #,###.##0
Negative value -0.1 #
Negative value -0.1 0
Negative value -0.13 0.#
Negative value -5000.123456789 #,##0.######
Localization format
US, UK and many more 1234567.890 #,##0.00
Estonia, France -128983833.4560022 ### ###,##
Germany, Italy -1234560.10002920 ##.000,00
Japan 963852741.001 ###,####.00
Switzerland 33445566.778899 #'###'#00.00
Any format
-1234.5678 ##.000,00
4651321.841 ##^000*00
3411.498 -##¿000$00
465456456.87987212 00!00@00
Force comma as separator 1112341.4348712 ###,###.
Force space as separator 2344441.4348712 ### ###.
Force dot as separator 2345341.4348712 ###.###
Prefix & Suffix
No spaces 123456789.9876 $#,##0.00USD
Extra spaces (set white-space: pre; in cells) 123456789.9876 $ #,##0.00 USD
123456789.9876 ##.000,00 €
123456789.9876 ###,####.00 ¥
123456789.9876 ### ###,### ¢ and stuff
123456789.9876 #,##0.00 a b c
Spaces & parenthesis (indicates a negative value, but the input is positive) 123456789.9876 $ (#,###.00) Money
Spaces & parenthesis (negative) -123456789.9876 $ (#,###.00) Money
Prefix with comma 123456789.9876 a, b c? #.00 yep!
Prefix with a periods 123456789.9876 cost... #,##0.00 yep!
Suffix with comma & period 123456789.9876 $# ###,00 USD, or euros.
Suffix with period 123456789.9876 It costs $# ###,00 euros.
Hanging decimal 123456789.9876 test:### ###. ing
Masks that don't work
No "#" outside of mask 123456789.9876 item #abc $#,###.00
No numbers outside of mask 123456789.9876 99 items = $#,###.00
No dashes outside of mask 123456789.9876 cost -- $#,###.00 -- value
No plus sign in prefix 123456789.9876 ++ value! $#,###.00
No plus sign in suffix 123456789.9876 $#,###.00 ++ value!


See also...