Garry's Mod Wiki

tonumber

  number or nil tonumber( string value, number base = 10 )

Description

Converts strings containing numbers into actual numbers.

Can also convert numbers from other numerical bases to base 10.

Arguments

1 string value
The value to be converted.

This string can contain digits from 0 to 9 (inclusive) for numerical bases from 2 to 10 (inclusive), and from 0 to z for bases greater than 10.

The maximum value depends on the specific base value provided, for example for base 3 0, 1, 2 are permitted. For base 11, 0-9 and a are permitted.

2 number base = 10
The numerical base of the digits in the input value.
Must be an integer between 2 and 36 (inclusive)

Returns

1 number or nil
The base 10 number representation of the input value, or nil if the conversion failed.

Example: String to number

Converts a string into a number

print( tonumber( "123" ) )
Output: 123

Example: Number base conversions

Converts a hexadecimal (base 16) and a binary (base 2) string into a base 10 number.

print( tonumber( "2CA88D", 16 ) ) -- 2926733 print( tonumber( "10010110", 2 ) ) -- 150
Output:
2926733 150