Advertisement
dalvorsn

dec2bin(number)

Apr 4th, 2014
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.45 KB | None | 0 0
  1. function dec2bin(number)
  2.     local bin = ""
  3.  
  4.     if type(number) == "number" then
  5.         local int, fract = math.floor(number), tostring(number):match("%d+\.(%d+)")
  6.  
  7.         while int > 0 do
  8.             bin = "" ..  (int % 2 ) .. bin
  9.             int = math.floor(int / 2)
  10.         end
  11.  
  12.         if fract then
  13.             bin = bin .. "."
  14.  
  15.             for i=1, #fract do
  16.                 local it = tonumber(fract:sub(i, i))
  17.                 bin = bin .. (it > 4 and "1" or "0")
  18.             end
  19.         end
  20.  
  21.         return bin
  22.     end
  23.  
  24.     return false
  25. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement