Advertisement
fames

lib

Dec 22nd, 2024
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.28 KB | None | 0 0
  1. function round(num, numDecimalPlaces)
  2.     local mult = 10^(numDecimalPlaces or 0)
  3.     num = num or 0
  4.     return math.floor(num * mult + 0.5) / mult
  5. end
  6.  
  7. function comma(number)
  8.  
  9.     local i, j, minus, int, fraction = tostring(number):find('([-]?)(%d+)([.]?%d*)')
  10.  
  11.     if number == nil or number == '' or int == nil then
  12.         return 0.0
  13.     end
  14.  
  15.     -- reverse the int-string and append a comma to all blocks of 3 digits
  16.     int = int:reverse():gsub("(%d%d%d)", "%1,")
  17.  
  18.     -- reverse the int-string back remove an optional comma and put the
  19.     -- optional minus and fractional part back
  20.     return minus .. int:reverse():gsub("^,", "") .. fraction
  21. end
  22.  
  23. function percentage(max,cur)
  24.     return (cur/max)*100
  25. end
  26.  
  27. function smooth(data, var, amount)
  28.     local amount = amount or 1
  29.     table.insert(data, var)
  30.  
  31.     if amount < 1 then amount = 1 end
  32.  
  33.     local dataLen = table.getn(data)
  34.     while dataLen > amount do
  35.         table.remove(data,1)
  36.         dataLen = table.getn(data)
  37.     end
  38.  
  39.     local sum = 0
  40.     local i = 0
  41.     for i=1, dataLen, 1 do
  42.         sum = sum + data[i]
  43.     end
  44.  
  45.     sum = (sum/dataLen)
  46.  
  47.     return sum
  48. end
  49.  
  50. function math.clamp(x, min, max)
  51.     if x < min then return min end
  52.     if x > max then return max end
  53.     return x
  54. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement