2024-12-12 05:43:07 +00:00
|
|
|
/**
|
|
|
|
* # Get Number from any input
|
|
|
|
* @example
|
|
|
|
* numberfy("123") // 123
|
|
|
|
* numberfy("123.456") // 123
|
|
|
|
* numberfy("123.456", 2) // 123.46
|
|
|
|
* numberfy("123.456", 0) // 123
|
|
|
|
* numberfy("123.456", 3) // 123.456
|
|
|
|
*/
|
2025-01-10 19:10:28 +00:00
|
|
|
export default function numberfy(num: any, decimals: number): number {
|
2024-12-12 05:43:07 +00:00
|
|
|
try {
|
|
|
|
const numberfiedNum = Number(num);
|
|
|
|
if (typeof numberfiedNum !== "number") return 0;
|
|
|
|
if (isNaN(numberfiedNum)) return 0;
|
|
|
|
if (decimals) return Number(numberfiedNum.toFixed(decimals));
|
|
|
|
return Math.round(numberfiedNum);
|
2025-01-10 19:10:28 +00:00
|
|
|
} catch (/** @type {any} */ error: any) {
|
2024-12-12 05:43:07 +00:00
|
|
|
console.log(`Numberfy ERROR: ${error.message}`);
|
|
|
|
return 0;
|
|
|
|
}
|
2025-01-10 19:10:28 +00:00
|
|
|
}
|