Number
toNum

🔢 Converts a value to a finite number.

Syntax

import { toNum } from '@opentf/std';
 
toNum(val: unknown): number

Parameters

  • val: The value to convert.

Returns

The converted number if the value is a valid numeric representation; otherwise, NaN.

Examples

toNum(1) //=> 1
 
toNum('1.3') //=> 1.3
 
toNum('1_000') //=> 1000 (Supports numeric separators)
 
toNum('0xF') //=> 15 (Supports hex strings)
 
toNum(Infinity) //=> NaN
 
toNum('abc') //=> NaN

Try