Logo

Programming-Idioms

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

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 = int(s)

The type int is unbounded

This raises an exception if s is invalid
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