Garry's Mod Wiki

Revision Difference

Global.tonumber#565624

<function name="tonumber" parent="Global" type="libraryfunc"> <description> 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)⤶ Converts strings containing numbers into actual <page>number</page>s. Can also convert numbers from other [numerical bases](https://www.mathsisfun.com/numbers/bases.html) to base 10. </description> <realm>Shared and Menu</realm> <args> <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. 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. </arg> <arg name="base" type="number" default="10"> The base of the digits in the input value. The numerical 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|nil"> The base `10` number representation of the input value, or `nil` if the conversion failed. </ret> </rets> </function> <example name="String to number"> <description> Converts a <page>string</page> into a <page>number</page> </description> <code> print( tonumber( "123" ) ) print( tonumber( "123" ) ) </code> <output> `123` </output> </example> <example name="Hexadecimal string to number">⤶ <example name="Number base conversions">⤶ <description> Converts a hexadecimal (base `16`) <page>string</page> into a base `10` <page>number</page>⤶ Converts a hexadecimal (base `16`) and a binary (base `2`) <page>string</page> into a base `10` <page>number</page>.⤶ </description> <code> print( tonumber( "2CA88D", 16 ) )⤶ print( tonumber( "2CA88D", 16 ) ) -- 2926733⤶ print( tonumber( "10010110", 2 ) ) -- 150⤶ </code> <output> `2926733`⤶ ```⤶ 2926733⤶ 150⤶ ```⤶ </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>⤶ </example>