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
Rounding to X digits
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="Ara.T.Howard, post: 4446149"] sorry- not only did i post the wrong version, but mine clobbered the existing truncate in Float, this ought to work: jib:~/eg/ruby > cat roundn.rb class Float def roundn sd = 2 f = self.floor prec = 10.0 ** sd.to_i.abs frac = (f == 0 ? self : self - f) f + (frac * prec).round / prec end end p(0.1 * (134.45/0.1).roundn) #=> 134.45 p((134.45 * (1/0.1)).roundn.to_f / (1/0.1)) #=> 134.45 jib:~/eg/ruby > ruby roundn.rb 134.45 134.45 regards. -a ps. i'm cc'ing you directly to let you know your message ends up as one big line in my mailer and i'm wondering if it's on my end or yours? -- =============================================================================== | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov | PHONE :: 303.497.6469 | When you do something, you should burn yourself completely, like a good | bonfire, leaving no trace of yourself. --Shunryu Suzuki =============================================================================== [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Ruby
Rounding to X digits
Top