Skip to content

Number Functions

This section contains utility functions for working with numbers.

isNumeric

Checks if a value is numeric.

/**
* Checks if a value is numeric
* @param value - The value to check
* @returns True if the value is numeric, false otherwise
*/
export function isNumeric(value: any): boolean {
return !isNaN(parseFloat(value)) && isFinite(value);
}

formatNumber

Formats a number with thousand separators.

/**
* Formats a number with thousand separators
* @param value - The number to format
* @param locale - The locale to use for formatting (default: 'en-US')
* @returns The formatted number string
*/
export function formatNumber(value: number, locale: string = 'en-US'): string {
return new Intl.NumberFormat(locale).format(value);
}

roundToDecimalPlaces

Rounds a number to a specified number of decimal places.

/**
* Rounds a number to a specified number of decimal places
* @param value - The number to round
* @param decimalPlaces - The number of decimal places to round to
* @returns The rounded number
*/
export function roundToDecimalPlaces(value: number, decimalPlaces: number): number {
const factor = Math.pow(10, decimalPlaces);
return Math.round(value * factor) / factor;
}