Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Ruby
Overriding Float.round
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="rpardee, post: 4512979"] Hey All, Looking at the google groups archive for this group, it seems that there's no built-in way to round a float to a particular number of decimal points. I thought I'd use that as an occasion to try my hand at extending a built-in class, and wrote this: class Float def round(places) num = ("1" + "0"*places.to_i).to_f (self*num).round/num end end But when I say: p = 22.0/7 puts p.round(3).to_s Ruby comes back with: C:/Documents and Settings/pardre1/Desktop/DelMe.rb:4:in `round': wrong number of arguments (0 for 1) (ArgumentError) from C:/Documents and Settings/pardre1/Desktop/DelMe.rb:4:in `round' from C:/Documents and Settings/pardre1/Desktop/DelMe.rb:9 What am I doing wrong? Thanks! -Roy [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Ruby
Overriding Float.round
Top