# Float calculation error

**URL:** <https://www.ruby-forum.com/t/float-calculation-error/64237>\
**Category:** Rails\
**Created:** [June 23, 2006, 1:17pm UTC](https://www.ruby-forum.com/t/float-calculation-error/64237 "2006-06-23T13:17:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ssssss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8dc957/32.png) [@ssssss](https://www.ruby-forum.com/u/ssssss)\
**Post date:** [June 23, 2006, 1:17pm UTC](https://www.ruby-forum.com/t/float-calculation-error/64237/1 "2006-06-23T13:17:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ssssss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8dc957/32.png) [@ssssss](https://www.ruby-forum.com/u/ssssss)\
**Post date:** [June 23, 2006, 2:02pm UTC](https://www.ruby-forum.com/t/float-calculation-error/64237/2 "2006-06-23T14:02:47Z")

</div>

2 ways:

1. store all your money in cents and do integer arithmetic (not prone to  
rounding errors)
2. use rounding

ss schrieb:

---

<div class="post-metadata">

**Author:** ![ssssss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8dc957/32.png) [@ssssss](https://www.ruby-forum.com/u/ssssss)\
**Post date:** [June 23, 2006, 2:55pm UTC](https://www.ruby-forum.com/t/float-calculation-error/64237/3 "2006-06-23T14:55:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ssssss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8dc957/32.png) [@ssssss](https://www.ruby-forum.com/u/ssssss)\
**Post date:** [June 23, 2006, 3:35pm UTC](https://www.ruby-forum.com/t/float-calculation-error/64237/4 "2006-06-23T15:35:31Z")

</div>

ss wrote:

> 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

---

<div class="post-metadata">

**Author:** ![ssssss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8dc957/32.png) [@ssssss](https://www.ruby-forum.com/u/ssssss)\
**Post date:** [June 23, 2006, 4:18pm UTC](https://www.ruby-forum.com/t/float-calculation-error/64237/5 "2006-06-23T16:18:34Z")

</div>

Richard L. 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:

> **[ActionView::Helpers::NumberHelper](https://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html#M000449)**
>
> Provides methods for converting numbers into formatted strings. Methods are provided for phone numbers, currency, percentage, precision, positional notation, file size, and pretty printing.

regards

Justin

---

<div class="post-metadata">

**Author:** ![ssssss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8dc957/32.png) [@ssssss](https://www.ruby-forum.com/u/ssssss)\
**Post date:** [June 23, 2006, 4:12pm UTC](https://www.ruby-forum.com/t/float-calculation-error/64237/6 "2006-06-23T16:12:08Z")

</div>

On 6/23/06, Pete [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![ssssss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8dc957/32.png) [@ssssss](https://www.ruby-forum.com/u/ssssss)\
**Post date:** [June 23, 2006, 6:54pm UTC](https://www.ruby-forum.com/t/float-calculation-error/64237/7 "2006-06-23T18:54:05Z")

</div>

Justin F. wrote:

> Richard L. 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:
> 
> [ActionView::Helpers::NumberHelper](http://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html#M000449)
> 
> regards
> 
> Justin

Also, facet/round\_at is super useful.

> gem install facets  
> require ‘facet/round\_at’  
> 3.12345.round\_at(2) #=\> 3.12
