UNPKG

diginext-utils

Version:
26 lines 617 B
import { isNull } from "./isNull.js"; /** * Converts a value to a floating-point number. * Returns 0 if the value cannot be converted. * * @param value - The value to convert * @returns The floating-point representation * * @example * ```ts * toFloat('3.14'); // 3.14 * toFloat('42'); // 42 * toFloat(5.7); // 5.7 * toFloat('hello'); // 0 * toFloat(null); // 0 * toFloat(''); // 0 * ``` */ export function toFloat(value) { if (isNull(value)) { return 0; } const result = parseFloat(String(value)); return Number.isNaN(result) ? 0 : result; } //# sourceMappingURL=toFloat.js.map