math.abs

Return the absolute value of num (i.e. the number's distance from zero). This means that negative numbers will be converted to a positive number (e.g. -5 into 5), and positive numbers remain unaffected. The return value will always be a positive number.

ExamplePrint the absolute value of user input
Read a line from the user, then print the absolute value of the number they entered.
Code
<nowiki>
print(math.abs(tonumber(read())))
    </nowiki>
Output Depends on what the user wrote. For instance, if they were to enter -5, the number printed would be 5.

math.abs
Function
Syntax
math.abs(
  • num : number
)

Returns number
API math
Source Lua (source)
This article is issued from Computercraft. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.