js 怎么截取后面2位小数点?

JavaScript025

js 怎么截取后面2位小数点?,第1张

在js中有时要用到取float数字的小数点后两位的情况。<br><br>var num=10/3<br>function f(num,n){<br>return parseint(num*math.pow(10,n)+0.5,10)/math.pow(10,n)<br>}<br>alert(f(num,2))<br><br>以上就是小编为大家带来的js取数字小数点后两位或n位的简单方法全部内容了,希望大家多多支持脚本之家~

小数点保留2位都可以用.toFixed(2)

改正后的zelc和Id函数如下(改动的地方见注释)

function zelc()

{

if((document.zobel.re.value <= 0 ) || (document.zobel.lces.value <= 0 ))

{

alert("请输入条件")

}

else

{

re = eval(document.zobel.re.value)

lces = eval(document.zobel.lces.value)

document.zobel.r1.value = eval(re*1.25).toFixed(2)//这里改一下

document.zobel.c1.value = eval((lces*1000000)/(re*re)).toFixed(2)//这里改一下

}

}

function ld()

{

if ((document.lp.impedance.value <=0) || (document.lp.attenuation.value <=0))

alert("请输入条件 !")

else

{

impedance = eval(document.lp.impedance.value)

attenuation = eval(document.lp.attenuation.value)

xval = eval(.05 * attenuation)

document.lp.r1.value = (impedance * (((Math.pow(10, xval)) - 1) / (Math.pow(10, xval)))).toFixed(2)//这里改一下

document.lp.r2.value = (impedance *( 1 / ((Math.pow(10, xval)) - 1))).toFixed(2)//这里改一下

}

}