Hi I have the following values pActualCost = 33.00 pPaymentCost = 29.99 So this calculation should leave me with 0.01 pPaymentDifference = pActualCost - pPaymentCost however when doing this in rails, it returns 0.00999999999999801 has anyone got any suggestions to whats going wrong and how I can correct this Thanks Scott -- Posted via http://www.ruby-forum.com/.
2 ways: 1. store all your money in cents and do integer arithmetic (not prone to rounding errors) 2. use rounding ss schrieb:> Hi > > I have the following values > > pActualCost = 33.00 > pPaymentCost = 29.99 > > > So this calculation should leave me with 0.01 > pPaymentDifference = pActualCost - pPaymentCost > > however when doing this in rails, it returns 0.00999999999999801 > > has anyone got any suggestions to whats going wrong and how I can > correct this > > Thanks > Scott > >
Pete wrote:> 2 ways: > > 1. store all your money in cents and do integer arithmetic (not prone to > rounding errors) > 2. use rounding > > ss schrieb:Thanks for anyone who is having a simular problem I used this to round to 2dp pPaymentDifference = (pPaymentDifference * 10**2).round.to_f / 10**2 -- Posted via http://www.ruby-forum.com/.
ss wrote:> Pete wrote: >> 2 ways: >> >> 1. store all your money in cents and do integer arithmetic (not prone to >> rounding errors) >> 2. use rounding >> >> ss schrieb: > > Thanks > > for anyone who is having a simular problem I used this to round to 2dp > > pPaymentDifference = (pPaymentDifference * 10**2).round.to_f / 10**2 >You could make it generic so that you don''t repeat yourself everywhere you need to round to decimal places. I have this in my app: class Float alias :round_off :round def round( d=0.0 ) rounded = (self * (10.0 ** d)).round_off.to_f / (10.0 ** d) rounded = rounded.to_i if d == 0.0 rounded end end -- R.Livsey http://livsey.org
On 6/23/06, Pete <pertl@gmx.org> wrote:> 2 ways: > > 1. store all your money in cents and do integer arithmetic (not prone to > rounding errors) > 2. use rounding >Or better yet, use BigDecimal from Ruby''s standard library. Isak> ss schrieb: > > Hi > > > > I have the following values > > > > pActualCost = 33.00 > > pPaymentCost = 29.99 > > > > > > So this calculation should leave me with 0.01 > > pPaymentDifference = pActualCost - pPaymentCost > > > > however when doing this in rails, it returns 0.00999999999999801 > > > > has anyone got any suggestions to whats going wrong and how I can > > correct this > > > > Thanks > > Scott > > > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Richard Livsey wrote:> ss wrote: >> Pete wrote: >>> 2 ways: >>> >>> 1. store all your money in cents and do integer arithmetic (not prone to >>> rounding errors) >>> 2. use rounding >>> >>> ss schrieb: >> >> Thanks >> >> for anyone who is having a simular problem I used this to round to 2dp >> >> pPaymentDifference = (pPaymentDifference * 10**2).round.to_f / 10**2 >> > > You could make it generic so that you don''t repeat yourself everywhere > you need to round to decimal places. > > I have this in my app: > > class Float > alias :round_off :round > def round( d=0.0 ) > rounded = (self * (10.0 ** d)).round_off.to_f / (10.0 ** d) > rounded = rounded.to_i if d == 0.0 > rounded > end > end >Rails already provides a helper, number_to_currency, described here: http://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html#M000449 regards Justin
Justin Forder wrote:> Richard Livsey wrote: >>> Thanks >> >> class Float >> alias :round_off :round >> def round( d=0.0 ) >> rounded = (self * (10.0 ** d)).round_off.to_f / (10.0 ** d) >> rounded = rounded.to_i if d == 0.0 >> rounded >> end >> end >> > > Rails already provides a helper, number_to_currency, described here: > > http://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html#M000449 > > regards > > JustinAlso, facet/round_at is super useful.> gem install facetsrequire ''facet/round_at'' 3.12345.round_at(2) #=> 3.12 -- Posted via http://www.ruby-forum.com/.