# Yet another rounding issue

**URL:** <https://www.ruby-forum.com/t/yet-another-rounding-issue/183844>\
**Category:** Ruby\
**Created:** [February 21, 2010, 6:39pm UTC](https://www.ruby-forum.com/t/yet-another-rounding-issue/183844 "2010-02-21T18:39:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![manusharma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/71e660/32.png) [@manusharma](https://www.ruby-forum.com/u/manusharma)\
**Post date:** [February 21, 2010, 6:39pm UTC](https://www.ruby-forum.com/t/yet-another-rounding-issue/183844/1 "2010-02-21T18:39:32Z")

</div>

At school I was taught that five is always rounded up. Later on I’ve  
been told that sometimes five is rounded to closest even number. Neither  
of these seem to be the case with ruby:

irb(main):044:0\> RUBY\_VERSION  
=\> “1.8.7”  
irb(main):045:0\> (217.5).round  
=\> 218  
irb(main):046:0\> (218.5).round  
=\> 219  
irb(main):047:0\> (2.175_100).round  
=\> 217  
irb(main):048:0\> (2.185_100).round  
=\> 219  
irb(main):049:0\> sprintf(’%.2f’,2.175)  
=\> “2.17”  
irb(main):050:0\> (2.175\*100.0).round/100.0  
=\> 2.17

## Can someone please tell me how do I round 2.175 to 2.18, besides ((2.175\*100).to\_s+‘9’).to\_f.round/100.0 ?

Manu S

---

<div class="post-metadata">

**Author:** ![manusharma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/71e660/32.png) [@manusharma](https://www.ruby-forum.com/u/manusharma)\
**Post date:** [February 21, 2010, 6:50pm UTC](https://www.ruby-forum.com/t/yet-another-rounding-issue/183844/2 "2010-02-21T18:50:26Z")

</div>

Hello,

> Can someone please tell me how do I round 2.175 to 2.18, besides  
> ((2.175\*100).to\_s+‘9’).to\_f.round/100.0 ?

I guess bigdecimal should be the answer

> > (2.175\*100).round  
> > =\> 217
> 
> > require ‘bigdecimal’  
> > =\> true
> 
> > a = BigDecimal(“2.175”)  
> > =\> #BigDecimal:1014bd230,‘0.2175E1’,8(8)
> 
> > (a\*100).round.to\_s  
> > =\> “0.218E3”

Cheers,

---

<div class="post-metadata">

**Author:** ![manusharma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/71e660/32.png) [@manusharma](https://www.ruby-forum.com/u/manusharma)\
**Post date:** [February 21, 2010, 6:51pm UTC](https://www.ruby-forum.com/t/yet-another-rounding-issue/183844/3 "2010-02-21T18:51:36Z")

</div>

On 22/2/2010 1:39 AM, Manu Sankala wrote:

> irb(main):047:0\> (2.175\*100).round

Actually, this is because of the way floating point variables are  
handled in a computer.

You could try:  
a = 2.175  
b = a + 0.0005  
c = ((b \* 100).round) / 100.0

irb(main):010:0\> a = 2.175  
=\> 2.175  
irb(main):011:0\> b = a + 0.0005  
=\> 2.1755  
irb(main):012:0\> c = (b \* 100).round / 100.0  
=\> 2.18  
irb(main):013:0\> a = 2.185  
=\> 2.185  
irb(main):014:0\> b = a + 0.0005  
=\> 2.1855  
irb(main):015:0\> c = (b \* 100).round / 100.0  
=\> 2.19

Cheers,  
Mohit.  
22/2/2010 | 1:46 AM.

---

<div class="post-metadata">

**Author:** ![manusharma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/71e660/32.png) [@manusharma](https://www.ruby-forum.com/u/manusharma)\
**Post date:** [February 21, 2010, 7:19pm UTC](https://www.ruby-forum.com/t/yet-another-rounding-issue/183844/4 "2010-02-21T19:19:19Z")

</div>

Mohit S. wrote:

> You could try:  
> a = 2.175  
> b = a + 0.0005  
> c = ((b \* 100).round) / 100.0

Well this is basically same as my  
((some\_float\_here\*100).to\_s+‘9’).to\_f.round/100.0  
except that in my version I don’t need to know how many decimals the  
float originally has. “b = a + 0.0005” would fail if ‘a’ has mode than 3  
decimals.

Since  
((float\_here\*100).to\_s+‘9’).to\_f.round/100.0  
and  
(BigDecimal(float\_here.to\_s)\*100).round.to\_s.to\_f/100  
seem to be about as fast I think I’ll go with bigfloat as it seems less  
like a hack.

Thank you both for your quick answers

- 

Manu S
