JavaScript的舍入数量精确到0.5 [英] Javascript roundoff number to nearest 0.5
本文介绍了JavaScript的舍入数量精确到0.5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人可以给我一个想法,我如何将数字四舍五入到最接近的0.5。
我必须根据屏幕分辨率缩放网页中的元素,以及我只能分配字体大小到1,1.5或2以上等。
Can someone give me an idea how can i round off a number to the nearest 0.5.
I have to scale elements in a web page according to screen resolution and for that i can only assign font size in pts to 1, 1.5 or 2 and onwards etc.
如果我将它舍入到小数点后1位或不填。
我该如何完成这项工作?
If i round off it rounds either to 1 decimal place or none. How can i accomplish this job?
推荐答案
编写自己的函数,乘以2,rounds,然后除以2,例如
Write your own function that multiplies by 2, rounds, then divides by 2, e.g.
function roundHalf(num) {
return Math.round(num*2)/2;
}
这篇关于JavaScript的舍入数量精确到0.5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文