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: 4446117"] jib:~/eg/ruby > cat truncate.rb class Float def truncate sd = 2 i = self.to_i prec = 10 ** sd fraction = (i.zero? ? self : (self / i) - 1) i + (fraction * prec).to_i / prec.to_f end end p(0.1 * (134.45/0.1).truncate) #=> 134.4 p((134.45 * (1/0.1)).truncate.to_f / (1/0.1)) #=> 134.4 jib:~/eg/ruby > ruby truncate.rb 134.4 134.4 regards. -a -- =============================================================================== | 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