Paul Jonathan Thompson
2008-Dec-03 02:04 UTC
Ruby can''t do arithmetic? How do I fix this?
Hi, I have this code @accept_ratio = @no_quotes / @no_accepted_quotes. I am deviding an integer by an integer but the resultant is not an iterger it should be floating to x decimal places. I was under the impression that ruby took care of this sort of thing. @no_quotes is 551 @no_accepted_quotes is 119 I am getting 4 in @accept_ratio which is wrong. It should be 4.63.... to however many decimal places. How do I persuade ruby not to chop the decimal off? I would like results to 2 decimal places. Thanks in advance, Paul Thompson --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Paul Jonathan Thompson wrote:> Hi, > > I have this code @accept_ratio = @no_quotes / @no_accepted_quotes. I > am deviding an integer by an integer but the resultant is not an > iterger it should be floating to x decimal places. I was under the > impression that ruby took care of this sort of thing. > > @no_quotes is 551 > @no_accepted_quotes is 119 > > I am getting 4 in @accept_ratio which is wrong. It should be 4.63.... > to however many decimal places. > > How do I persuade ruby not to chop the decimal off? I would like > results to 2 decimal places. > > Thanks in advance, > > Paul Thompson > >Change one of them to float. Ruby uses integer arithmetic with integers. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Paul Jonathan Thompson
2008-Dec-03 02:20 UTC
Re: Ruby can''t do arithmetic? How do I fix this?
Hi Norm, I am not sure how to do that. I have tried .to_f but that did not make any difference. Paul 2008/12/3 Norm <normscherer-ihVZJaRskl1bRRN4PJnoQQ@public.gmane.org>:> > Paul Jonathan Thompson wrote: >> Hi, >> >> I have this code @accept_ratio = @no_quotes / @no_accepted_quotes. I >> am deviding an integer by an integer but the resultant is not an >> iterger it should be floating to x decimal places. I was under the >> impression that ruby took care of this sort of thing. >> >> @no_quotes is 551 >> @no_accepted_quotes is 119 >> >> I am getting 4 in @accept_ratio which is wrong. It should be 4.63.... >> to however many decimal places. >> >> How do I persuade ruby not to chop the decimal off? I would like >> results to 2 decimal places. >> >> Thanks in advance, >> >> Paul Thompson >> >> > Change one of them to float. Ruby uses integer arithmetic with integers. > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Paul Jonathan Thompson wrote:> Hi Norm, > > I am not sure how to do that. I have tried .to_f but that did not make > any difference. > > Paul >When you say you tried .to_f do you mean you tried @accept_ratio = @no_quotes.to_f / @no_accepted_quotes.to_f It works for me. You only really need to convert either of the arguments. @accept_ratio.to_f = @no_quotes / @no_accepted_quotes will not work because it will first evaluate to the integer 4 and then convert that to a float.... --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Paul Jonathan Thompson
2008-Dec-03 02:54 UTC
Re: Ruby can''t do arithmetic? How do I fix this?
Thanks Norm, Your second example is what I did (@accept_ratio.to_f = @no_quotes / @no_accepted_quote) so it did not work. but when I changed it to how you showed inyour first example, it worked just fine. Thank you very much. Paul 2008/12/3 Norm <normscherer-ihVZJaRskl1bRRN4PJnoQQ@public.gmane.org>:> > Paul Jonathan Thompson wrote: >> Hi Norm, >> >> I am not sure how to do that. I have tried .to_f but that did not make >> any difference. >> >> Paul >> > When you say you tried .to_f do you mean you tried > > @accept_ratio = @no_quotes.to_f / @no_accepted_quotes.to_f > > It works for me. You only really need to convert either of the arguments. > > @accept_ratio.to_f = @no_quotes / @no_accepted_quotes will not work because it will first evaluate to the integer 4 and then convert that to a float.... > > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---