js数值计算

var nndjh = new Number($("#ndjhs").val());
var nbyxs = new Number(cols[2].firstChild.value);
var nyxszb = nbyxs/nndjh;
obj.value = nyxszb.toFixed(4)*100;

将值转换成数值
除计算
取4位小数,再*100得百分数

常用操作
//格式化数字
function formatNum(obj){
var num = obj.value;
if(num.length == 0 || num == "" || ($.trim(num)).length == 0)return;
obj.value = toNum(num);
}

//转换成2两小数,返是字符串
function toNum(num){
var tem = new Number(num);
var num2 = tem.toFixed(2);
return num2;
}

//验证是否数字,可以为空串
function numValidtae(obj,msg){
var num = obj.value;
if(num == "" || isNum(num)){
return;
}
else{
alert("[" + msg + "]只能是数字");
obj.value="";
obj.focus();
}
}

//验证是否数字
function isNum(num){
var reg = /^-?\d+\.{0,}\d{0,}$/;
if (!reg.exec(num)) {
return false;
}
return true;
}

你可能感兴趣的:(js)