Lua abs()

Anonymous contributor's avatar
Anonymous contributor
Published Oct 23, 2023
Contribute to Docs

The abs() function in Lua’s math library returns the absolute value of a given number.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Lua, a general-purpose programming language used for building games, web apps, and developer tools.
    • Beginner Friendly.
      4 hours

Syntax

math.abs(number)
  • number: The number to be addressed.

Example

This example demonstrates a basic implementation of math.abs().

local x = -5
local answer = math.abs(x)
local output = string.format("Absolute value %.0f is %.4f", x, answer)
print(output)

This will result in the following output below.

Absolute value -5 is 5.0000

All contributors

Contribute to Docs

Learn Lua on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Lua, a general-purpose programming language used for building games, web apps, and developer tools.
    • Beginner Friendly.
      4 hours