Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lua: Rounding numbers and then truncate

Which is the best efficient way to round up a number and then truncate it (remove decimal places after rounding up)?

for example if decimal is above 0.5 (that is, 0.6, 0.7, and so on), I want to round up and then truncate (case 1). Otherwise, I would like to truncate (case 2)

for example: 232.98266601563 => after rounding and truncate = 233 (case 1) 232.49445450000 => after rounding and truncate = 232 (case 2) 232.50000000000 => after rounding and truncate = 232 (case 2) 
like image 531
Ralph Avatar asked Aug 19 '13 11:08

Ralph


People also ask

How do you round and truncate?

To round down, the formula is =ROUNDDOWN(num,digits) . When Excel truncates, it chops off part of the entered number and performs no rounding at all. So if you input 345.679 and format the number for no decimal points, it cuts off the digits after the decimal point.

Is truncation better than rounding?

Truncation is a method of approximating numbers. It is easier than rounding, but does not always give the best approximation to the original number. Truncation is used in computing when division is done with integers and the answer must be an integer.

Does round () round up or down?

Rules for Rounding Here's the general rule for rounding: If the number you are rounding is followed by 5, 6, 7, 8, or 9, round the number up. Example: 38 rounded to the nearest ten is 40. If the number you are rounding is followed by 0, 1, 2, 3, or 4, round the number down.


1 Answers

There is no build-in math.round() function in Lua, but you can do the following: print(math.floor(a+0.5)).

like image 172
Ola M Avatar answered Sep 22 '22 12:09

Ola M