Advertisement
BobMe

Complete round function

Nov 27th, 2019
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. -- superscript generator (for numbers)
  2.  
  3. function to_super(x)
  4. local function number_to_super(x)
  5. local y = tonumber(x)
  6. if y ~= nil then
  7. local tab = {"¹","²","³","⁴","⁵","⁶","⁷","⁸","⁹"}
  8. if tab[y] ~= nil then
  9. return tab[y]
  10. else
  11. return "⁰"
  12. end
  13. end
  14. end
  15. local y = tostring(x)
  16. local str = ""
  17. for i=1,#y do
  18. str = str..number_to_super(string.sub(y,i,i))
  19. end
  20. return str
  21. end
  22.  
  23. -- im going to make a complete rounding function
  24.  
  25. function round(number,deci)
  26. local num = tostring(number)
  27. local num2 = ""
  28. local num3 = ""
  29. local num4
  30. local savevar1 = ""
  31. for i=1,#num do
  32. if string.sub(num,i,i+1):lower() == "e+" then
  33. savevar1 = string.sub(num,i+2)
  34. num2 = string.sub(num,1,i-1)
  35. break
  36. end
  37. end
  38. if num2 ~= "" then num = num2 end
  39. for i=1,#num do
  40. if string.sub(num,i,i) == "." then
  41. num3 = tonumber(string.sub(num,deci+i+1,deci+i+1))
  42. num4 = tonumber(string.sub(num,deci+i,deci+i))
  43. if num3 == "" or num3 == nil then break end
  44. if num3 >= 5 then
  45. num = string.sub(num,1,i-1+deci)..tostring(num4+1)
  46. else
  47. num = string.sub(num,1,i-1+deci)..tostring(num4)
  48. end
  49. end
  50. end
  51. if savevar1 ~= "" then
  52. num = num.." * 10"..to_super(savevar1)
  53. end
  54. return num
  55. end
  56.  
  57. print(round((8213.2791),2));print(round((9.32874723 * 17^39),2))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement