문제

I am aware of what the ceil function does, it rounds numbers up. So; 1.3 to 2.0 and 5.9 to 6.0. But I want it to round up with steps of 0.5. So; 1.3 to 1.5 and 5.9 to 6.0. Is this possible? Thanks!!!

도움이 되었습니까?

해결책

y = ceil(x * 2.0)/2.0;

should do what you need:

x     x*2.0   ceil(x*2.0)  y
------------------------------
1.3    2.6     3.0         1.5
1.6    3.2     4.0         2.0
5.9   11.8    12.0         6.0
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top