When handling percentage calculations:
Simple 99/100..
Will I always have to do 99.to_f/100.to_f in order to receive 0.99? Or
is there a simpler way of performing this task in ruby..
I notice that there doesn't appear to be a round method with decimal
places (or perhaps I can't find one) so I found this one which appears
to work:
class Float
def round_to(x)
(self * 10**x).round.to_f / 10**x
end
def ceil_to(x)
(self * 10**x).ceil.to_f / 10**x
end
def floor_to(x)
(self * 10**x).floor.to_f / 10**x
end
end
The question I have is can I expand upon divisor behavior and create
something that changes the way ruby behaves with integers/floats on a
more permanent basis?
I only ask because I'm working with standard deviation and some advanced
mathematics that revolve around calculating variance. I just don't want
to experience any gotchas halfway down my project.
Lastly, is there a good core site that houses all of the API for ruby
math functions or refined methods that deal with variance?
Many thanks.
···
--
Posted via http://www.ruby-forum.com/.
99.0/100 is sufficient here
···
On Jul 16, 2009, at 11:55 PM, Älphä Blüë wrote:
When handling percentage calculations:
Simple 99/100..
Will I always have to do 99.to_f/100.to_f in order to receive 0.99? Or
is there a simpler way of performing this task in ruby..
Älphä Blüë wrote:
When handling percentage calculations:
Simple 99/100..
Will I always have to do 99.to_f/100.to_f in order to receive 0.99? Or
is there a simpler way of performing this task in ruby..
or you use
require 'mathn'
and I bet, there is much stuff in the facets-pakage: http://facets.rubyforge.org/
99.0/100 or 99/100.0 will also work! You would have to keep one floating
point to get a floating point result!
regards,
Vikas Sarin
Senior Software Quality Engineer
QA Infotech
Email: vikassarin@qainfotech.net
Mob#: +919810319641
···
-----Original Message-----
From: Ralf Mueller [mailto:ralf.mueller@zmaw.de]
Sent: Friday, July 17, 2009 10:51 AM
To: ruby-talk ML
Subject: Re: I have some Ruby math questions that need answering
Älphä Blüë wrote:
When handling percentage calculations:
Simple 99/100..
Will I always have to do 99.to_f/100.to_f in order to receive 0.99? Or
is there a simpler way of performing this task in ruby..
or you use
require 'mathn'
and I bet, there is much stuff in the facets-pakage:
http://facets.rubyforge.org/
Thanks everyone - I appreciate the information. This does help me out
going forward. I'll look into facets as well.
···
--
Posted via http://www.ruby-forum.com/.