2016-11-05 81 views
0

我需要一些帮助将Swift中的浮点数向上或向下舍入到最接近的偶数。你会如何在Swift 3中将浮点值向上或向下舍入到最接近的偶数整数?

如:

32.86 would be closest to 32 

33.86 would be closest to 34 
+1

会发生什么'33.0'? – Keiwan

+2

类似:[Round Double to nearest 10](http://stackoverflow.com/questions/27922406/round-double-to-closest-10-swift),[最接近五的圆形货币](http:// stackoverflow。 COM /问题/ 35613696 /轮货币上最近五的)。 –

回答

4

如果要通过2轮四舍五入到最接近的偶数,除然后乘以2:

let rounded = Int(round(value/2.0)) * 2 
相关问题