Hi,
I am new using corona SDK, and I want to round a number with 2 digits after the dot.
Like this: 5.44444 -> 5.44 , and 10.113 -> 10.11
I found only this function (math.round) that round all my number
Thank you in advance 
Hi,
I am new using corona SDK, and I want to round a number with 2 digits after the dot.
Like this: 5.44444 -> 5.44 , and 10.113 -> 10.11
I found only this function (math.round) that round all my number
Thank you in advance 
-- == -- round(val, n) - Rounds a number to the nearest decimal places. (http://lua-users.org/wiki/FormattingNumbers) -- val - The value to round. -- n - Number of decimal places to round to. -- == local function round(val, n) if (n) then return math.floor( (val \* 10^n) + 0.5) / (10^n) else return math.floor(val+0.5) end end
print( round( 1.5678 ) ) – prints: 1.57
Some good info about string formatting :
https://coronalabs.com/blog/2015/02/17/tutorial-formatting-values-using-string-format/
-- == -- round(val, n) - Rounds a number to the nearest decimal places. (http://lua-users.org/wiki/FormattingNumbers) -- val - The value to round. -- n - Number of decimal places to round to. -- == local function round(val, n) if (n) then return math.floor( (val \* 10^n) + 0.5) / (10^n) else return math.floor(val+0.5) end end
print( round( 1.5678 ) ) – prints: 1.57
Some good info about string formatting :
https://coronalabs.com/blog/2015/02/17/tutorial-formatting-values-using-string-format/