Lua轉換爲int

Lua轉換爲int

https://www.codenong.com/10962085/

Lua string to int

如何在Lua中將字符串轉換爲整數?

我有一個像這樣的字符串:

1

a ="10"

我希望將其轉換爲10。

相關討論

 


使用tonumber函數。與a = tonumber("10")中一樣。

相關討論

 


您可以通過在a="10" + 0中的算術運算中使用字符串來強制進行隱式轉換,但這並不像顯式使用tonumber那樣清晰。

相關討論

 

 

 


1
2
3
4

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

輸出量

1
2

   string                                                                                                                                                                          
   number


Lua中的所有數字均爲浮點數(編輯:Lua 5.2或更小)。如果您確實要轉換爲" int"(或至少複製此行爲),則可以執行以下操作:

1
2
3

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

在這種情況下,您可以將字符串(或實際上,無論它是什麼)顯式轉換爲數字,然後像Java中的(int)強制轉換一樣截斷數字。

編輯:即使在Lua 5.3包含實整數的情況下,它仍然可以在Lua 5.3中使用,因爲math.floor()返回整數,而如果number是浮點數,則諸如number // 1的運算符仍將返回浮點數。


說您要轉換爲數字的字符串在變量S中

1

a=tonumber(S)

假設有數字並且只有S中的數字,它將返回一個數字,
但是,如果有任何不是數字的字符(浮點數除外)
它將返回nil


更清楚的選擇是使用tonumber。

從5.3.2開始,此函數將自動檢測(帶符號)整數,浮點數(如果存在點)和十六進制(整數和浮點數,如果字符串以" 0x"或" 0X"開頭)。

以下代碼段較短,但不等效:

  • 1

    a + 0 -- forces the conversion into float, due to how + works.

  • 1
    2

    a | 0 -- (| is the bitwise or) forces the conversion into integer.
    -- However, unlike `math.tointeger`, it errors if it fails.

 

 


您可以使訪問器將" 10"保留爲int 10。

例:

1

x = tonumber("10")

如果打印x變量,它將輸出int 10而不是" 10"

像Python過程一樣

x = int(" 10")

謝謝。


我建議檢查Hyperpolyglot,它有一個很棒的比較:http://hyperpolyglot.org/

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

ps。實際上,Lua轉換爲雙精度而不是整數。

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

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


應當注意,math.floor()始終四捨五入,因此對於負浮點值不會產生明智的結果。

例如,表示爲整數的-10.4通常會被截斷或舍入爲-10。但是math.floor()的結果並不相同:

1

math.floor(-10.4) => -11

對於使用類型轉換的截斷,以下輔助函數將起作用:

1
2
3
4

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

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


tonumber (e [, base])

tonumber具有兩個參數,第一個是轉換爲數字的字符串,第二個是e的基數。

返回值tonumber以10爲底。

如果未提供base,它將數字轉換爲10。

1
2
3

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

如果提供了base,它將其轉換爲給定的base。

1
2
3
4
5
6
7
8
9
10
11

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

如果e包含無效字符,則返回nil。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

> --[[ 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

我回答了考慮使用Lua5.3


1
2
3
4
5
6
7
8
9

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

 

 


這是你應該放的東西

1
2
3
4
5
6
7

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

output:

20

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章