當我們想要獲得一個數字的整數值以及該數字的小數值(如果有的話)時,有幾次出現,以便我們可以使用這些值中的一個或兩個。
Lua 為我們提供了一個math.modf()我們可以使用該函數來查找整數值和小數值(如果數字有的話)。
用法
math.modf(number)
這個math.modf()當我們調用函數時,函數返回兩個值,第一個值是數字的整數值,第二個返回值是數字的小數值(如果有的話)。
示例
讓我們考慮一個簡單的例子,我們將在其中使用math.modf()Lua 中的函數 -
a, b = math.modf(3.3)
c, d = math.modf(7.1)
print(a, b)
print(c, d)
輸出
3 0.3 7 0.1
需要注意的是,如果我們試圖找到modf已經是與其自身最接近的整數的數字,那麽我們將獲得與輸出相同的數字。
示例
考慮下麵顯示的例子 -
e, f = math.modf(8)
print(e, f)
輸出
8 0.0
我們也可以在math.modf()函數作為參數。
示例
考慮下麵顯示的例子 -
g, h = math.modf(-3.3)
print(g, h)
輸出
-3 -0.3
相關用法
- Lua string.char()用法及代碼示例
- Lua string.gsub()用法及代碼示例
- Lua table.pack()用法及代碼示例
- Lua string.byte()用法及代碼示例
- Lua io.popen()用法及代碼示例
- Lua string.lower()用法及代碼示例
- Lua string.format()用法及代碼示例
- Lua table.unpack()用法及代碼示例
- Lua string.upper()用法及代碼示例
- Lodash _.isValidDate()用法及代碼示例
- Lodash _.isInteger()用法及代碼示例
- Lodash _.sampleSize()用法及代碼示例
- Lodash _.fromQuery()用法及代碼示例
- Lodash _.noConflict()用法及代碼示例
- Lodash _.Intersection()用法及代碼示例
- Lodash _.values()用法及代碼示例
- Less isstring()用法及代碼示例
- Lodash _.isLength()用法及代碼示例
- Lodash _.third()用法及代碼示例
- Lodash _.negate()用法及代碼示例
注:本文由純淨天空篩選整理自Mukul Latiyan大神的英文原創作品 math.modf() function in Lua programming。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。