Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


Abs

Returns the absolute value of f.
Read time 1 minuteLast updated 4 days ago

Definition

  • Type: Method
  • Namespace: UnityEngine
  • Assembly: UnityEngine.CoreModule

Abs(float)

Returns the absolute value of f.
public static float Abs(float f)

Parameters

Returns

Type

Description

float

Examples

using UnityEngine;public class ScriptExample : MonoBehaviour{ void Start() { // Prints 10 Debug.Log(Mathf.Abs(-10.0f)); // Prints 0 Debug.Log(Mathf.Abs(0.0f)); // Prints 10 Debug.Log(Mathf.Abs(10.0f)); }}

Abs(int)

Returns the absolute value of
value
.
public static int Abs(int value)

Parameters

value

Integer number whose absolute value will be returned.

Returns

Type

Description

intNon-negative value of
value
.

Remarks

For an integer number
value
, its absolute value is defined as its non-negative value without regard to its sign.
Additional Resources: Mathf.Sign.

Examples

using UnityEngine;public class Example : MonoBehaviour{ void Start() { // Prints 10 Debug.Log(Mathf.Abs(-10)); // Prints 0 Debug.Log(Mathf.Abs(0)); // Prints 10 Debug.Log(Mathf.Abs(10)); }}