Guest User

Untitled

a guest
Jul 22nd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.41 KB | None | 0 0
  1. class Float
  2. def to_s( base = 2, maxdigitafterpoint = 10 )
  3. integer = truncate
  4.  
  5. res = integer.to_s( base ) + "."
  6.  
  7. i = 0
  8. fraction = self - integer
  9. while i < maxdigitafterpoint and fraction != 0.0
  10. fraction *= base
  11. digit = fraction.truncate
  12. res += digit.to_s
  13. fraction -= digit
  14.  
  15. ++i
  16. end
  17.  
  18. res
  19. end
  20. end
  21.  
  22. puts 103.625.to_s
  23. puts 103.626.to_s(2,80)
Add Comment
Please, Sign In to add comment