Advertisement
Guest User

Untitled

a guest
May 7th, 2014
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.73 KB | None | 0 0
  1. --- Rounds a number to the given decimal place
  2. -- @param num
  3. -- @param idp
  4. --
  5. function round(num, idp)
  6.     local mult = 10^(idp or 0)
  7.     return math.floor(num * mult + 0.5) / mult
  8. end
  9.  
  10. --- Converts a number to a shorter string; e.g. 1234 to 1.23k
  11. -- @param num
  12. -- @param idp
  13. --
  14. function numberToShortString(num, idp)
  15.     num = num or 0
  16.     idp = idp or 2
  17.  
  18.     if num / 1000000000 > 1 then
  19.         return string.format("%.fb", round(num / 1000000000, idp))
  20.     elseif num / 1000000 > 1 then
  21.         return string.format("%.fm", round(num / 1000000, idp))
  22.     elseif num / 1000 > 1 then
  23.         return string.format("%.fk", round(num / 1000, idp))
  24.     else
  25.         return string.format("%.f", round(num, idp))
  26.     end
  27. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement