Posted By

vanne on 02/07/07


Tagged

ruby


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

webstic


decimal method for float objects


 / Published in: Ruby
 

Extend any float object to allow for decimal conversion

  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
Posted By: gangbird on September 29, 2007

def ftodec(f, prec=2,sep='.') return f.toi.tos+sep+((f*(10prec)).toi%(10prec)).tos end

You need to login to post a comment.