簡易四捨五入

lua-users home
wiki

使用

function DIV(a,b)
    return (a - a % b) / b
end

在除法中得到整數結果。(JlnWntr)

以下函式將數值四捨五入到給定的位數小數。

function round(num, numDecimalPlaces)
  local mult = 10^(numDecimalPlaces or 0)
  return math.floor(num * mult + 0.5) / mult
end

這裡有一個備用實作

function round2(num, numDecimalPlaces)
  return tonumber(string.format("%." .. (numDecimalPlaces or 0) .. "f", num))
end

兩者都與 Lua 5.0 與 5.1 相容。

如果將數值四捨五入用於列印,請移除 tonumber:轉換為數值再轉換回字串會重新產生四捨五入誤差。

測試

> function test(a, b) print(round(a,b), round2(a,b)) end
> test(43245325.9995, 3)
43245326        43245325.999
> test(43245325.9994, 3)
43245325.999    43245325.999
> test(43245325.5543654)
43245326        43245326
> test(43245325.5543654, 3)
43245325.554    43245325.554
> test(43245325.5543654, 4)
43245325.5544   43245325.5544

注意:如果 numDecimalPlaces 為負值,第一個函式會異常動作,因此這個版本可能較穩定 (Robert Jay Gould)


實際上我需要四捨五入到 100,因此負值情況非常有用且在數學上是正確的(沒有錯誤)

round(1023.4345, -2) = 1000
round(1023.4345, 2) = 1023.43
(Tom P.)
function round(num, numDecimalPlaces)
  if numDecimalPlaces and numDecimalPlaces>0 then
    local mult = 10^numDecimalPlaces
    return math.floor(num * mult + 0.5) / mult
  end
  return math.floor(num + 0.5)
end


function round(num) return math.floor(num+.5) end
-- Rob

-.5 可能會有意料之外的結果嗎?請見下方。

-- Henning


無條件取捨
    function round(num) 
        if num >= 0 then return math.floor(num+.5) 
        else return math.ceil(num-.5) end
    end

注意 math.ceil(num-.5) ~= math.floor(num+.5),例如 -.5 使用 math.ceil(num-.5) 為 -1,math.floor(num+.5) 為 0

範例:1.1 -> 1、1 -> 1、0.9 -> 1、0.5 -> 1、0.1 -> 0、0 -> 0、-0.1 -> 0、-0.4 -> 0、-0.5 -> -1、-0.6 -> -1、-0.9 -> -1、-1: -1、-1.1: -1

-- Henning


function round(num)
    under = math.floor(num)
    upper = math.floor(num) + 1
    underV = -(under - num)
    upperV = upper - num
    if (upperV > underV) then
        return under
    else
        return upper
    end
end


從上方範例結合而成 - 無條件取捨並具有精度
function round(num, numDecimalPlaces)
    local mult = 10^(numDecimalPlaces or 0)
    if num >= 0 then return math.floor(num * mult + 0.5) / mult
    else return math.ceil(num * mult - 0.5) / mult end
end

-- Igor Skoric (i.skoric@student.tugraz.at)


我為自己的遊戲製作了一個小實作
function round(n, mult)
    mult = mult or 1
    return math.floor((n + mult/2)/mult) * mult
end


Pan 處理負值取餘 >上方原始範例

--should round and give negatives too if I'm not mistaken
function round(num, numDecimalPlaces)
  if numDecimalPlaces and numDecimalPlaces>0 then
    local mult = 10^numDecimalPlaces
    return math.floor(num * mult + 0.5) / mult
  else
    numDecimalPlaces = numDecimalPlaces mult 2 -- negates its negative status
    local mult = 10^numDecimalPlaces
    return math.floor(num * mult + 0.5) / mult
    ide = numDecimalPlaces / (numDecimalPlaces/2)
  end
  return math.floor(num + 0.5)
end

我不太會 lua 反而幫了我,不太懂上面的範例。

Math.modf 是你的好朋友!

if num<0 then x=-.5 else x=.5 end
Integer, decimal = math.modf(num+x)
然後整數會等於 num,無條件取捨為正負值。

對於四捨五入到小數位,我使用

roundto=10
if num*roundto<0 then x=-.5 else x=.5 end
Integer, decimal = math.modf(num*roundto+x)
result = Integer/roundto

結果會等於四捨五入到小數位「roundto」的數值


SoniEx2 處理 0.49999999999999994(至少在 Java 中。但沒有特別處理負數和/或小數位)
function round(n)
  return math.floor((math.floor(n*2) + 1)/2)
end

(Luc Bloom)

function math.sign(v)
	return (v >= 0 and 1) or -1
end
function math.round(v, bracket)
	bracket = bracket or 1
	return math.floor(v/bracket + math.sign(v) * 0.5) * bracket
end
使用這種方式,你可以對任何方括號進行四捨五入
math.round(119.68, 6.4) -- 121.6 (= 19 * 6.4)
它也適用於「小數位數」,並且有相當直觀的顯示方式
math.round(119.68, 0.01) -- 119.68
math.round(119.68, 0.1) -- 119.7
math.round(119.68) -- 120
math.round(119.68, 100) -- 100
math.round(119.68, 1000) -- 0

我想知道誰要製作下一個四捨五入函式?-- Lucids


RecentChanges · 選項
編輯 · 歷史
最後編輯時間:2021 年 9 月 17 日下午 2:35 GMT (差異)