Lua string to int

Go To StackoverFlow.com

151

How can I convert a string to an integer in Lua? Thank you.

I have a string like this:

a = "10"

I would like it to be converted to 10, the number.

2012-06-09 15:09
by David Gomes
The precise link is the section on coercion: 5.1, 5.2 - lhf 2012-06-09 18:53
@NicolBolas: +1 for asking a question that is the first Google result and provides a straight-to-the point answer - ereOn 2014-10-09 18:52
+1 to compensate this biting troll Nicol : - Петър Петров 2014-10-27 14:02
Lua just does automatically conversion between strings and numbers. If you want ensure the type, use a = tonumber(a) - xpol 2016-02-26 03:52


250

Use the tonumber function. As in a = tonumber("10").

2012-06-09 15:14
by Nicol Bolas
In Lua 5.3, (64-bit default) integers are treated accordingly (http://www.lua.org/manual/5.3/manual.html): "A numeric constant with a fractional dot or an exponent denotes a float; otherwise it denotes an integer. - Kevin Lee 2015-03-19 15:10


30

You can force an implicit conversion by using a string in an arithmetic operations as in a= "10" + 0, but this is not quite as clear or as clean as using tonumber explicitly.

2012-06-09 18:52
by lhf
Nope, it'll convert "10" to integer and then add 0 to it. (The lack of clarity is all the more reason to use tonumber instead, though! - Rena 2015-06-28 06:32
@Rena, there's no lack in clarity. + is always explicitly addition, .. - concatenation - Oleg V. Volkov 2016-02-20 12:36
@lhf: auto coercion will only work on numbers. And comparison operators (== ~= < > <= >=) do not convert their arguments. And for performance reasons you should avoid relying on automatic coercion too muc - wsha 2018-09-20 07:36


7

All numbers in Lua are floats (edit: Lua 5.2 or less). If you truly want to convert to an "int" (or at least replicate this behavior), you can do this:

local function ToInteger(number)
    return math.floor(tonumber(number) or error("Could not cast '" .. tostring(number) .. "' to number.'"))
end

In which case you explicitly convert the string (or really, whatever it is) into a number, and then truncate the number like an (int) cast would do in Java.

Edit: This still works in Lua 5.3, even thought Lua 5.3 has real integers, as math.floor() returns an integer, whereas an operator such as number // 1 will still return a float if number is a float.

2014-08-28 15:08
by Stormswept


7

local a = "10"
print(type(a))
local num = tonumber(a)
print(type(num))

Output

   string                                                                                                                                                                          
   number
2015-03-26 16:34
by NoName


4

say the string you want to turn into a number is in the variable S

a=tonumber(S)

provided that there are numbers and only numbers in S it will return a number, but if there are any characters that are not numbers (except periods for floats) it will return nil

2015-03-12 16:36
by CORE craftX


4

The clearer option is to use tonumber.

As of 5.3.2, this function will automatically detect (signed) integers, float (if a point is present) and hexadecimal (both integers and floats, if the string starts by "0x" or "0X").

The following snippets are shorter but not equivalent :

  • a + 0 -- forces the conversion into float, due to how + works.
    
  • a | 0 -- (| is the bitwise or) forces the conversion into integer. 
    -- However, unlike `math.tointeger`, it errors if it fails.
    
2016-04-29 18:13
by 4xel


2

I would recomend to check Hyperpolyglot, has an awesome comparison: http://hyperpolyglot.org/

http://hyperpolyglot.org/more#str-to-num-note

ps. Actually Lua converts into doubles not into ints.

The number type represents real (double-precision floating-point) numbers.

http://www.lua.org/pil/2.3.html

2014-11-28 16:19
by Marcs


2

You can make an accessor to keep the "10" as int 10 in it.

Example:

x = tonumber("10")

if you print the x variable, it will output an int 10 and not "10"

same like Python process

x = int("10")

Thanks.

2015-07-12 09:19
by user5555332


1

It should be noted that math.floor() always rounds down, and therefore does not yield a sensible result for negative floating point values.

For example, -10.4 represented as an integer would usually be either truncated or rounded to -10. Yet the result of math.floor() is not the same:

math.floor(-10.4) => -11

For truncation with type conversion, the following helper function will work:

function tointeger( x )
    num = tonumber( x )
    return num < 0 and math.ceil( num ) or math.floor( num )
end

Reference: http://lua.2524044.n2.nabble.com/5-3-Converting-a-floating-point-number-to-integer-td7664081.html

2018-07-17 18:05
by Leslie Krause


0

Lua 5.3.1  Copyright (C) 1994-2015 Lua.org, PUC-Rio
> math.floor("10");
10
> tonumber("10");
10
> "10" + 0;
10.0
> "10" | 0;
10
2018-07-01 14:49
by Viacheslav


-1

here is what you should put

local stringnumber = "10"
local a = tonumber(stringnumber)
print(a + 10)

output:

20
2016-12-16 22:41
by theresaspyaroundhere
a = tonumber(stringnumber) is sufficen - wsha 2018-09-20 07:29
Ads