I’m modifying numbers to have 2 decimal places after calculations for cash values - so for values like 0.075 it would be $0.08 (rounded up). I’m using math.floor and it works for the most part but I’m seeing a weird result running the code below:
0.075 —> rounds correctly to 0.08
0.575 —> rounds INCORRECTLY to 0.57 (should be 0.58)
1.075 —> rounds correctly to 1.08
it looks like math.floor(58) = 57 when calculated from the values above it, but it’s correct math.floor(58)=58 in the print statement.
Is there something I’m missing or doing incorrect here?
This is in Corona Version 2013.1251 (2013.11.1):
local function round(val, decimal)
if (decimal) then
local part1 = (val * 10^decimal)
local part2 = part1+0.50
local part3 = math.floor(part2)
print("-----------------------------------------------")
print(“multiplying it =”,part1)
print(“adding 0.5 =”,part2)
print("** math.floor to above =",part3)
print(“math.floor(58)=”,math.floor(58))
print("------------------------------------------------")
local finalval = part3 / (10^decimal)
return finalval
else
return math.floor(val+0.5)
end
end
print(“rounded value 0.075=”,round(0.075,2))
print(“rounded value 0.575=”,round(0.575,2))
print(“rounded value 1.075=”,round(1.075,2))