Number: Difference between revisions
>Pighead10 No edit summary |
>NecroBumpist Rewrite a lot. Update to use the Lua template. Add info about different types of literals (decimal, hex). Maybe someone else could better explain the concept of hex, but I've linked wikipedia in the meantime. |
||
(17 intermediate revisions by 5 users not shown) | |||
Line 1: | Line 1: | ||
{{Map|Scripting|Data Types}} | |||
{{CatUp|Data Types}} | {{CatUp|Data Types}} | ||
A '''Number''' in Lua is a [http://en.wikipedia.org/wiki/Double_precision_floating-point_format double precision floating point] number (or just 'double'). Every Lua variable that is simply a number (not a Vector3, just a number) with or without decimal places, negative or positive, is a double. | A '''Number''' in Lua is a [http://en.wikipedia.org/wiki/Double_precision_floating-point_format double precision floating point] number (or just 'double'). Every Lua variable that is simply a number (not a Vector3, just a number) with or without decimal places, negative or positive, is a double. | ||
== Literals == | |||
There are several ways you can include numbers into your [[Lua]] [[script]]. All of these ways include ''literals'', which are values that will be used in your program '''exactly''' as they are seen in the source. | |||
Once they are included into your [[script]], you can perform [[Operator|manipulate and operate]] on these numbers. | |||
=== Standard Literals === | |||
The following are valid numbers literals: | |||
{{lua|= | |||
5 | |||
9.12761656 | |||
-1927 | |||
}} | |||
Doubles can range from 1.7E–308 to 1.7E+308 (that's around 15 digits, positive or negative). In most cases, this is easily big enough for what you need it for (15 digits is about one hundred trillion, so you won't need much bigger than that). It is not possible to go out of this limit. | |||
''NOTE:'' All of the above examples are in [http://en.wikipedia.org/wiki/Decimal base-10, or decimal]. | |||
=== Hexadecimal Literals === | |||
[[Lua]] is also capable of accepting number literals in [http://en.wikipedia.org/wiki/Hexadecimal base-16, or hexadecimal]. | |||
While decimal numbers include the characters {{`|0}} through {{`|9}}, hexadecimal numbers use {{`|0}} through {{`|F}}. | |||
To declare a number literal as hexadecimal (hex for short), prefix it with {{`|0x}}. | |||
The following are valid hex number literals: | |||
{| class="wikitable" | |||
! Hex !!Decimal | |||
|- | |||
||0x1234||4660 | |||
|- | |||
||0xABCD||43981 | |||
|- | |||
||0xface||64206 | |||
|} | |||
== ROBLOX Usage == | |||
Certain Properties in Roblox require a whole number, or a positive number. In such cases an [[Integer]] will be asked for, instead of a number. If the number in your script doesn't have a decimal place and fits within the range of a Roblox property that required an integer, it will be converted to an integer. | Certain Properties in Roblox require a whole number, or a positive number. In such cases an [[Integer]] will be asked for, instead of a number. If the number in your script doesn't have a decimal place and fits within the range of a Roblox property that required an integer, it will be converted to an integer. | ||
== See Also == | |||
* [[Integer|Integers]] | |||
* [[Operators]] | |||
* [http://en.wikipedia.org/wiki/Decimal base-10] | |||
* [http://en.wikipedia.org/wiki/Hexadecimal base-16] | |||
[[Category:Data | [[Category:Data types]] |
Latest revision as of 21:59, 15 April 2012
A Number in Lua is a double precision floating point number (or just 'double'). Every Lua variable that is simply a number (not a Vector3, just a number) with or without decimal places, negative or positive, is a double.
Literals
There are several ways you can include numbers into your Lua script. All of these ways include literals, which are values that will be used in your program exactly as they are seen in the source.
Once they are included into your script, you can perform manipulate and operate on these numbers.
Standard Literals
The following are valid numbers literals:
5
9.12761656
-1927
Doubles can range from 1.7E–308 to 1.7E+308 (that's around 15 digits, positive or negative). In most cases, this is easily big enough for what you need it for (15 digits is about one hundred trillion, so you won't need much bigger than that). It is not possible to go out of this limit.
NOTE: All of the above examples are in base-10, or decimal.
Hexadecimal Literals
Lua is also capable of accepting number literals in base-16, or hexadecimal. While decimal numbers include the characters 0 through 9, hexadecimal numbers use 0 through F.
To declare a number literal as hexadecimal (hex for short), prefix it with 0x.
The following are valid hex number literals:
Hex | Decimal |
---|---|
0x1234 | 4660 |
0xABCD | 43981 |
0xface | 64206 |
ROBLOX Usage
Certain Properties in Roblox require a whole number, or a positive number. In such cases an Integer will be asked for, instead of a number. If the number in your script doesn't have a decimal place and fits within the range of a Roblox property that required an integer, it will be converted to an integer.