Garry's Mod Wiki

Revision Difference

Global.tonumber#565623

<function name="tonumber" parent="Global" type="libraryfunc"> <description> Attempts to convert the value to a number. Converts strings containing numbers into numbers. ⤶ Can also convert numbers in other bases to base `10`.⤶ ⤶ To learn more about numerical bases, [click here.](https://www.mathsisfun.com/numbers/bases.html)⤶ </description> <realm>Shared and Menu</realm> <args> <arg name="value" type="any">The value to convert. Can be a number or string.</arg>⤶ <arg name="base" type="number" default="10">The base used in the string. Can be any integer between 2 and 36, inclusive.</arg>⤶ <arg name="value" type="string">⤶ The value that will be converted. ⤶ ⤶ This string can contain digits from `0` to `9` (inclusive) for bases from `2` to `10` (inclusive) ⤶ For bases greater than `10`, digits can be values from `0` to `z`, depending on the specific base value provided.⤶ </arg>⤶ <arg name="base" type="number" default="10">⤶ The base of the digits in the input value. ⤶ Must be an integer between `2` and `36` (inclusive)⤶ ⤶ For most situations, the default value of `10` is correct and this argument can be left empty.⤶ </arg>⤶ </args>⤶ <args name="Number Base Conversion">⤶ <arg name="value" type="number">⤶ The value that will be converted.⤶ </arg>⤶ <arg name="base" type="number" default="10">⤶ The numerical base of the input value. ⤶ Must be an integer between `2` and `36` (inclusive)⤶ ⤶ For most situations, the default value of `10` is correct and this argument can be left empty.⤶ </arg>⤶ </args> <rets> <ret name="" type="number">The numeric representation of the value with the given base, or <page>nil</page> if the conversion failed.</ret>⤶ <ret name="" type="number|nil">⤶ The base `10` number representation of the input value, or `nil` if the conversion failed.⤶ </ret>⤶ </rets> </function> ⤶ <example>⤶ <description>Convert a string to a number.</description>⤶ <code>print(tonumber("123"))</code>⤶ <output>123</output>⤶ ⤶ <example name="String to number">⤶ <description>⤶ Converts a <page>string</page> into a <page>number</page>⤶ </description>⤶ <code>⤶ print( tonumber( "123" ) )⤶ </code>⤶ <output>⤶ `123`⤶ </output>⤶ </example>⤶ ⤶ <example name="Hexadecimal string to number">⤶ <description>⤶ Converts a hexadecimal (base `16`) <page>string</page> into a base `10` <page>number</page>⤶ </description>⤶ <code>⤶ print( tonumber( "2CA88D", 16 ) )⤶ </code>⤶ <output>⤶ `2926733`⤶ </output>⤶ </example>⤶ ⤶ <example name="Binary number to decimal number">⤶ <description>⤶ Converts a binary (ones and zeroes) <page>number</page> into a decimal (base `10`) <page>number</page>.⤶ </description>⤶ <code>⤶ print( tonumber( 10010110, 2 ) )⤶ </code>⤶ <output>⤶ `150`⤶ </output>⤶ </example>