Skip to content

About

string to integer conversion module

Resources

Stars

1 star

Watchers

1 watching

Forks

Latest commit

 

History

13 Commits

Folders and files

Repository files navigation

lua-tointeger

test codecov

string to integer conversion module.

Installation

luarocks install tointeger

API

iv = tointeger( v )

converts v to an integer and returns it. If v is not convertible to an integer, returns nil.

The conversion follows the same rules as Lua 5.4 math.tointeger:

input type result
integer the integer itself
float with no fractional part the truncated integer
float with a fractional part nil
numeric string convertible to an integer the parsed integer
numeric string convertible to a non-integer float nil
any other type nil

Parameters

  • v:any: value to convert.

Returns

  • iv:integer|nil: the resulting integer, or nil if v is not convertible.

Example

local tointeger = require('tointeger')

-- strings are parsed and converted
print(tointeger('123'))    -- 123
print(tointeger('-456'))   -- -456
print(tointeger('1.5'))    -- nil (fractional part)

-- numbers are checked for a fractional part
print(tointeger(42))       -- 42
print(tointeger(42.0))     -- 42
print(tointeger(3.14))     -- nil (fractional part)

-- non-numeric values return nil
print(tointeger('abc'))    -- nil
print(tointeger(true))     -- nil

About

string to integer conversion module

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages