Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rounding a float to the nearest .5 [duplicate]

Tags:

rounding

ios

I need to round a float to the nearest .5, for example:

1.1 = 1
1.5 = 1.5
1.6 = 1.5
1.8 = 2

and so on. I really don't know how to do it. Any help would be greatly appreciated.

like image 518
Aleph72 Avatar asked Sep 14 '25 10:09

Aleph72


1 Answers

For positive numbers Multiply by 2, add 0.5, truncate, divide by 2.

like image 54
Michael Dorgan Avatar answered Sep 16 '25 00:09

Michael Dorgan