WoW:USERAPI round: Difference between revisions

m
no edit summary
(Wow, the old one was overly complicated.)
mNo edit summary
Line 13: Line 13:


== Example ==
== Example ==
  local number = round(math.pi, 2)
  local number = round(math.pi, 3)
  print(number) -- prints 3.14
  print(number) -- prints 3.142


==Code==
==Code==
<!-- No, no sanity checks are needed. That's the programmers responsibility. -->
<!-- No, no sanity checks are needed. That's the programmers responsibility. -->
<pre>function round(number, decimals)
<pre>function round(number, decimals)
return number - (number % (0.1 ^ decimals))
    return (("%%.%df"):format(decimals)):format(number)
end</pre>
end</pre>
== See Also ==
* [[truncate]]
__NOTOC__
__NOTOC__
Anonymous user