The math.modf function splits a number into its integral and fractional parts.

Syntax

math.modf(x)

Parameters

  • x
    A number (float) that you want to split into integral and fractional parts.

Return value

Returns two values:

  • The integral part of x (as a float).
  • The fractional part of x (as a float).

Description

math.modf is useful for obtaining both the whole number and decimal parts of a floating-point number. The integral part is the largest integer less than or equal to x, while the fractional part is the difference between x and its integral part.

Examples

Using math.modf

local intPart, fracPart = math.modf(3.14)
print(intPart)  -- Output: 3.0
print(fracPart)  -- Output: 0.14

Handling negative numbers

local intPart, fracPart = math.modf(-2.5)
print(intPart)  -- Output: -2.0
print(fracPart)  -- Output: -0.5

See also

  • math.floor
  • math.ceil

Was this page helpful?

Table of Contents