Advertisement
p0rt23

format.lua

Jul 23rd, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. local format = {}
  2.  
  3. local format.round(f,p)
  4. if p == nil then
  5. if f < 1 then p = 2
  6. elseif f < 10 then p = 2
  7. elseif f < 100 then p = 1
  8. elseif f < 10000 then p = 1
  9. else p = 0
  10. end
  11. end
  12. if p > 0 then
  13. return(math.floor(f*10^p+0.5)/10^p)
  14. else
  15. return(math.floor(f+0.5))
  16. end
  17. end
  18.  
  19. local format.formatNumber(n)
  20. if n < 10^3 then n = format.round(n)
  21. elseif n < 10^6 then n = format.round(n/10^3) .."k"
  22. elseif n < 10^9 then n = format.round(n/10^6) .."M"
  23. elseif n < 10^12 then n = format.round(n/10^9) .."B"
  24. else n = format.round(n/10^12).."T"
  25. end
  26. return n
  27. end
  28.  
  29. return format
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement