asNumberOr

Utility

Converts a string to a number, or returns a fallback value if the conversion is not possible.

Usage

asNumberOr("42");        // Returns 42
asNumberOr("123.45");    // Returns 123.45
asNumberOr("abc", 10);   // Returns 10
asNumberOr("NaN");       // Returns 0
asNumberOr("NaN", 5);    // Returns 5

Props

value

The string to convert to a number.

type

string

fallback?

The value to return if the conversion is not possible. Defaults to 0.

type

number