decimal method for float objects


/ Published in: Ruby
Save to your folder(s)

Extend any float object to allow for decimal conversion


Copy this code and paste it in your HTML
  1. class Float
  2. # USAGE : 123123123.123131.decimal # => 123123123.12
  3. # OR 123123123.123131.decimal 4 # => 123123123.1231
  4. # Defaults to a scale of 2
  5. def decimal(prec=2)
  6. number = self.to_s.split(".")[0]
  7. scale = self.to_s.split(".")[1][0..(prec-1)]
  8. "#{number}.#{scale}".to_f
  9. end
  10. end

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.