# Round the floating point to nearest halfpoint

**URL:** <https://www.ruby-forum.com/t/round-the-floating-point-to-nearest-halfpoint/189195>\
**Category:** Ruby\
**Created:** [May 26, 2010, 2:40pm UTC](https://www.ruby-forum.com/t/round-the-floating-point-to-nearest-halfpoint/189195 "2010-05-26T14:40:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lucky1234](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d07c76/32.png) [@lucky1234](https://www.ruby-forum.com/u/lucky1234)\
**Post date:** [May 26, 2010, 2:40pm UTC](https://www.ruby-forum.com/t/round-the-floating-point-to-nearest-halfpoint/189195/1 "2010-05-26T14:40:58Z")

</div>

Hi ,  
I want to round the my floating point result into nearest halfpoint  
.how to do it.  
Example:  
1)My floating point result is 3.3  
i want it become 3.5  
2) My floating point result is 3.6  
i want it become 4

How can we do it

---

<div class="post-metadata">

**Author:** ![lucky1234](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d07c76/32.png) [@lucky1234](https://www.ruby-forum.com/u/lucky1234)\
**Post date:** [May 26, 2010, 3:13pm UTC](https://www.ruby-forum.com/t/round-the-floating-point-to-nearest-halfpoint/189195/2 "2010-05-26T15:13:55Z")

</div>

On 26 May 2010 15:09, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> robert

Arf, you answered before I did finish my answer.

… was looking the exact names for rounding in BigDecimal.mode …

Regards,  
B.D.

---

<div class="post-metadata">

**Author:** ![lucky1234](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d07c76/32.png) [@lucky1234](https://www.ruby-forum.com/u/lucky1234)\
**Post date:** [May 26, 2010, 3:09pm UTC](https://www.ruby-forum.com/t/round-the-floating-point-to-nearest-halfpoint/189195/3 "2010-05-26T15:09:49Z")

</div>

2010/5/26 Lucky Nl [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I want to round the my floating point result into nearest halfpoint  
> .how to do it.  
> Example:  
> 1)My floating point result is 3.3  
> i want it become 3.5  
> 2) My floating point result is 3.6  
> i want it become 4

If you are rounding then 3.6 should yield 3.5 because (3.6 - 3.5) \<\<  
(4 - 3.6). Otherwise you want the ceiling function.

> How can we do it

irb(main):012:0\> [3.3, 3.6].map {|x| (x \* 2).round / 2.0}  
=\> [3.5, 3.5]  
irb(main):014:0\> [3.3, 3.6].map {|x| (x \* 2).ceil / 2.0}  
=\> [3.5, 4.0]

Btw, I would rather not round float values and use them as they are.  
If at all I would usually only round them for output.

Cheers

robert
