Convert API
formatNum()
Format a number with grouped thousands and optional control over decimal places, decimal separator, and thousand separator.
- Accepts a number or string as the first argument to be formatted.
-
Accepts an optional integer as the second argument, specifying the number of decimal places to include.
The default value is "2". -
Accepts an optional string as the third argument, specifying the decimal separator to use.
The default value is ".". -
Accepts an optional string as the fourth argument, specifying the thousands separator to use.
The default value is " " (space). - Returns the formatted number as a string, or false if the input is invalid.
Example:
formatNum('book');
// false
formatNum(1000.55);
formatNum('1000.55');
formatNum(1000.55, 2, '.', ' ');
// '1 000.55'
formatNum(1000.55, 2, '.', ',');
// '1,000.55'
formatNum(1000.55, 0);
// '1 000'
formatNum(1234567.89, 4, '.', '');
// '1234567.8900'