Lua string to int

后端 未结 12 1277
礼貌的吻别
礼貌的吻别 2020-12-23 08:39

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

I have a string like this:

a = \"10\"

I would like it to be converted to 10, the n

相关标签:
12条回答
  • 2020-12-23 09:18

    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

    0 讨论(0)
  • 2020-12-23 09:19
    local a = "10"
    print(type(a))
    local num = tonumber(a)
    print(type(num))
    

    Output

       string                                                                                                                                                                          
       number
    
    0 讨论(0)
  • 2020-12-23 09:25

    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

    0 讨论(0)
  • 2020-12-23 09:26

    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.

    0 讨论(0)
  • 2020-12-23 09:28

    tonumber (e [, base])

    tonumber takes two arguments, first is string which is converted to number and second is base of e.

    Return value tonumber is in base 10.

    If no base is provided it converts number to base 10.

    > a = '101'
    > tonumber(a)
    101
    

    If base is provided, it converts it to the given base.

    > a = '101'
    > 
    > tonumber(a, 2)
    5
    > tonumber(a, 8)
    65
    > tonumber(a, 10)
    101
    > tonumber(a, 16)
    257
    > 
    

    If e contains invalid character then it returns nil.

    > --[[ Failed because base 2 numbers consist (0 and 1) --]]
    > a = '112'
    > tonumber(a, 2)
    nil
    > 
    > --[[ similar to above one, this failed because --]]
    > --[[ base 8 consist (0 - 7) --]]
    > --[[ base 10 consist (0 - 9) --]]
    > a = 'AB'
    > tonumber(a, 8)
    nil
    > tonumber(a, 10)
    nil
    > tonumber(a, 16)
    171
    

    I answered considering Lua5.3

    0 讨论(0)
  • 2020-12-23 09:30

    here is what you should put

    local stringnumber = "10"
    local a = tonumber(stringnumber)
    print(a + 10)
    
    output:
    
    20
    
    0 讨论(0)
提交回复
热议问题