Omsigames

grapes/Number.lua

Dec 25th, 2024
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.87 KB | None | 0 0
  1.  
  2. local number = {}
  3.  
  4. --------------------------------------------------------------------------------
  5.  
  6. function number.round(num)
  7.     if num >= 0 then
  8.         return math.floor(num + 0.5)
  9.     else
  10.         return math.ceil(num - 0.5)
  11.     end
  12. end
  13.  
  14. function number.roundToDecimalPlaces(num, decimalPlaces)
  15.     local mult = 10 ^ (decimalPlaces or 0)
  16.     return number.round(num * mult) / mult
  17. end
  18.  
  19. function number.getDigitCount(num)
  20.     return num == 0 and 1 or math.ceil(math.log(num + 1, 10))
  21. end
  22.  
  23. function number.shorten(num, digitCount)
  24.     if num < 1000 then
  25.         return num
  26.     else
  27.         local shortcuts = { "K", "M", "G", "T", "P", "E", "Z", "Y" }
  28.         local index = math.floor(math.log(num, 1000))
  29.  
  30.         return number.roundToDecimalPlaces(num / 1000 ^ index, digitCount) .. shortcuts[index]
  31.     end
  32. end
  33.  
  34. --------------------------------------------------------------------------------
  35.  
  36. return number
  37.  
Tags: ORMS
Add Comment
Please, Sign In to add comment