将值舍入到最接近的1/2 [英] rounding a value to nearest 1/2
问题描述
有没有人有一个将数字四舍五入为0或.5的函数?
我有一个表格,我输入一个以英寸为单位的数字。我需要把它四舍五入到最近的1/2英寸(onChange)
。
分割将以.25
为增量
22.24 = 22.0
22.25 = 22.5
22.52 = 22.5
22.74 = 22.5
22.751 = 23.0
TIA!
Does anyone have a function that will round a number to 0 or .5?
I have a form where I''m entering a number in inches. I need to round it to
the nearest 1/2 inch (onChange).
The split will be on increments of .25
22.24 = 22.0
22.25 = 22.5
22.52 = 22.5
22.74 = 22.5
22.751 = 23.0
TIA!
推荐答案
" calan" <无** @ nospam.com>写道:
"calan" <no**@nospam.com> writes:
有没有人有一个将数字舍入为0或.5的函数?
Does anyone have a function that will round a number to 0 or .5?
函数roundHalf(n ){
返回Math.round(n * 2)/ 2;
}
/ L
-
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML死亡颜色:< URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
''没有判断的信仰只会降低精神神圣。''
function roundHalf(n) {
return Math.round(n*2)/2;
}
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
''Faith without judgement merely degrades the spirit divine.''
calan写道:
有没有人有一个将数字舍入为0或.5的函数?
如何关于这个
Math.round((22.25 * 2))/ 2)
我有一个表格,我输入一个以英寸为单位的数字。我需要将它四舍五入到最近的1/2英寸(onChange)。
分割将以.25的增量进行。
22.24 = 22.0
22.25 = 22.5
22.52 = 22.5
22.74 = 22.5
22.751 = 23.0
TIA!
Does anyone have a function that will round a number to 0 or .5?
How about this
Math.round((22.25*2))/2)
I have a form where I''m entering a number in inches. I need to round it to
the nearest 1/2 inch (onChange).
The split will be on increments of .25
22.24 = 22.0
22.25 = 22.5
22.52 = 22.5
22.74 = 22.5
22.751 = 23.0
TIA!
-
- 。
- =<> Clue博士(AKA Ian A. Storms)<> = - C ++,HTML / CSS,Javascript,TCP ......
- `
--
--.
--=<> Dr. Clue (A.K.A. Ian A. Storms) <>=-- C++,HTML/CSS,Javascript,TCP ...
--`
calan在消息新闻中写道:og ***************** @ newssvr12.news.prodigy.co m ...
calan wrote in message news:og*****************@newssvr12.news.prodigy.co m...
任何人都有一个将数字舍入为0或.5的函数?
我有一个表格,我输入一个以英寸为单位的数字。我需要将它四舍五入到最近的1/2英寸(onChange)。
分割将以.25的增量进行。
22.24 = 22.0
22.25 = 22.5
22.52 = 22.5
22.74 = 22.5
22.751 = 23.0
Does anyone have a function that will round a number to 0 or .5?
I have a form where I''m entering a number in inches. I need to round it to
the nearest 1/2 inch (onChange).
The split will be on increments of .25
22.24 = 22.0
22.25 = 22.5
22.52 = 22.5
22.74 = 22.5
22.751 = 23.0
这些我喜欢的舍入函数使用:
函数DecimalRound(DValue,DPrecision){
返回Math.round(DValue / DPrecision)* DPrecision;
}
document.write(" 22.24 - >" + DecimalRound(22.24,0.5)+"< br>");
document.write(" 22.25 - >" + DecimalRound(22.25,0.5)+"< br>");
document.write(" 22.52 - > " + DecimalRound(22.52,0.5)+" br>");
document.write(" 22.74 - >" + DecimalRound(22.74,0.5)+" < br>");
document.write(" 22.751 - >" + DecimalRound(22.751,0.5)+"< br>< br>");
这样,如果你是你需要改变一个不同的精度:
document.write(" 22.24 - > " + DecimalRound(22.24,0.25)+"< br>");
document.write(" 22.25 - >" + DecimalRound(22.25,0.25)+" < br>");
document.write(" 22.52 - >" + DecimalRound(22.52,0.25)+"< br>");
document.write(" 22.74 - >" + DecimalRound(22.74,0.25)+"< br>");
document.write(" 22.751 - >" + DecimalRound(22.751,0.25)+"< br>");
HTH
for those kinds of rounding functions I like to use:
function DecimalRound(DValue, DPrecision){
return Math.round(DValue / DPrecision) * DPrecision;
}
document.write("22.24 -> "+DecimalRound(22.24, 0.5)+"<br>");
document.write("22.25 -> "+DecimalRound(22.25, 0.5)+"<br>");
document.write("22.52 -> "+DecimalRound(22.52, 0.5)+"<br>");
document.write("22.74 -> "+DecimalRound(22.74, 0.5)+"<br>");
document.write("22.751 -> "+DecimalRound(22.751, 0.5)+"<br><br>");
this way, if you need a different precision, you just change it:
document.write("22.24 -> "+DecimalRound(22.24, 0.25)+"<br>");
document.write("22.25 -> "+DecimalRound(22.25, 0.25)+"<br>");
document.write("22.52 -> "+DecimalRound(22.52, 0.25)+"<br>");
document.write("22.74 -> "+DecimalRound(22.74, 0.25)+"<br>");
document.write("22.751 -> "+DecimalRound(22.751, 0.25)+"<br>");
HTH
>
这篇关于将值舍入到最接近的1/2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!