isinteger

isinteger

function isinteger( i ) --> res

Description

Checks if the argument i is an integer or not.

Parameters

i

Argument to check.

Return Values

res

true if the argument is and integer, otherwise false.

Code

--ZFUNC-isinteger-v0
local function isinteger( i ) --> res
   if "number" ~= type( i ) then return false end
   local i, f = math.modf( i )
   return ( 0 == f )
end

return isinteger

Examples

local t = require "taptest"
local isinteger = require "isinteger"

t( isinteger( 1 ),      true )
t( isinteger( 0 ),      true )
t( isinteger( 1.1 ),   false )
t( isinteger( "1" ),   false )
t( isinteger( true ),  false )
t( isinteger( { 1 } ), false )
t( isinteger(),        false )

t()