Normalize Numeric Value
Note
Normalize a value to a number
if possible.
β¨ Descriptionβ
normalizeNumber
accepts either a number or a numeric string and safely converts it to a number
.
If the input is not a valid number or a numeric string, it returns undefined
.
π¦ Importβ
import { normalizeNumber } from 'nhb-toolbox';
π§ Signatureβ
function normalizeNumber(num: unknown): number | undefined
Parameter | Type | Description |
---|---|---|
num | unknown | A value that might be a number or a numeric string. |
Returns:
number | undefined
β A normalized number, or undefined
if the input is invalid.
β Examplesβ
normalizeNumber(42);
// β 42
normalizeNumber('123');
// β 123
normalizeNumber('12.5');
// β 12.5
normalizeNumber('abc');
// β undefined
normalizeNumber(true);
// β undefined
π‘ Notesβ
- Useful when you accept input from forms, query params, or external data sources where values might be strings.
- Automatically handles both integers and floatingβpoint numeric strings.
π See Alsoβ
- isNumber - How number is checked in this function
- isNumericString - How numeric string is checked in this function