Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Ruby

Idiom #345 Convert string to big integer

Create the integer value i initialized from its string representation s (in radix 10)

Use an integer type that can hold huge values. Explain what happens if s cannot be parsed.

i = s.to_i

Extraneous characters past the end of a valid number are ignored. If there is not a valid number at the start of s, 0 is returned. This never raises an exception.
import "math/big"
i := new(big.Int)
_, ok := i.SetString(s, 10)

If s cannot be parsed, then ok will be false

New implementation...
< >
programming-idioms.org