Skip to main content

Function: fromString()

fromString(input): Result<number, ParseError>

Defined in: number/fromString/index.ts:35

Parses a string into a number.

Returns Ok with the parsed number, or Err with ParseError if parsing fails. Handles common number formats including decimals, negatives, and scientific notation.

Parameters

input

string

The string to parse

Returns

Result<number, ParseError>

Result containing the parsed number or an error

Example

fromString("123") // => Ok(123)
fromString("123.45") // => Ok(123.45)
fromString("-42") // => Ok(-42)
fromString("1.23e4") // => Ok(12300)
fromString("abc") // => Err({ code: 'PARSE_ERROR', ... })
fromString("") // => Err({ code: 'PARSE_ERROR', ... })

// Real-world: Form input validation
const validatePrice = (input: string) =>
pipe(
fromString(input),
flatMap(price =>
price > 0 ? ok(price) : err({ code: 'INVALID_PRICE' })
)
)

See

fromCurrency - for parsing currency strings