Function createNumber

  • Create a number from a number-like value.

    • If the value is a number, it is returned as-is.
    • If the value is a bigint, it is converted to a number.
    • If the value is a string, it is interpreted as a decimal number.
    • If the value is a hex string (i.e., it starts with "0x"), it is interpreted as a hexadecimal number.

    This validates that the value is a number-like value, and that the resulting number is not NaN or Infinity.

    Example

    const value = createNumber('0x010203');
    console.log(value); // 66051

    const otherValue = createNumber(123n);
    console.log(otherValue); // 123

    Returns

    The created number.

    Throws

    If the value is not a number-like value, or if the resulting number is NaN or Infinity.

    Parameters

    • value: string | number | bigint

      The value to create the number from.

    Returns number