Skip to content

Instantly share code, notes, and snippets.

@paladini
Created January 21, 2015 04:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paladini/b92a3ba6899ef3b1b172 to your computer and use it in GitHub Desktop.
Save paladini/b92a3ba6899ef3b1b172 to your computer and use it in GitHub Desktop.
This method truncates a float or double number in Ruby programming language.
# This method truncate a float / double number.
def truncate(number, decimalCases = 2)
temp = 1.0
while decimalCases > 0 do
temp = temp * 10.0
decimalCases -= 1
end
return (number * temp.to_i).floor / temp
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment