Number Utility Functions
A set of helper functions for formatting, validating, and generating numeric values.
numberFormat(number: number, decimals?: number, decimalSeparator?: string, thousandsSeparator?: string): string
Formats a number with custom decimal places, separators, and thousands grouping. Similar to PHP's number_format()
.
Parameters:
number
: The number to format.decimals
(optional): Number of decimal digits. Defaults to0
.decimalSeparator
(optional): Character to use as decimal point. Defaults to"."
.thousandsSeparator
(optional): Character to use for thousands. Defaults to","
.
Returns:
- A string of the formatted number.
Example:
untitled
numberFormat(1234567.891, 2, ".", ","); // "1,234,567.89"
numberFormat(1234567.891, 3, ",", "."); // "1.234.567,891"
NumberOnly(value: any): number
toNumberOnly(value: any): number
Extracts numeric content from a string and converts it to a number. Removes non-numeric characters except the first decimal point.
Parameters:
value
: Any input that may represent a number.
Returns:
- A valid number, or
0
if invalid.
Example:
untitled
NumberOnly("abc123.45def"); // 123.45
NumberOnly("12.34.56"); // 12.3456
NumberOnly("abc"); // 0
NumberOnly(null); // 0
randomNumber(len?: number): string
Generates a pseudo-random numeric string of a specified length.
Parameters:
len
(optional): The number of digits. Defaults to a full-length random string.
Returns:
- A numeric string.
Example:
untitled
randomNumber(6); // e.g., "385721"