[英]ceiling decimal format in javascript
I want to get specified format number.. help me plz.. 我想获取指定的格式号..帮帮我。
I made function 我做了功能
function ceilingAbsolute(inVal, pos){
var digits = Math.pow(10, pos);
var num = 0;
var patten = "[0-9]{"+pos+"}$";
var re = new RegExp(patten, "");
num = inVal.replace(re);
num = num * digits;
return num;
}
var testVal = ceilingAbsolute(255555, 3) ;
I have expected testVal = 255000, but got "255undefined" .. 我期望testVal = 255000,但是得到了“ 255undefined”。
I want to get ceiling deciaml number.. 我想获取上限deciaml号码。
somebody help please.. 请有人帮忙..
The reason you're getting 255undefined
is that you are not passing a replacement value to the replace
function along with the regex. 得到
255undefined
的原因是您没有将替换值与正则表达式一起传递给replace
函数。 Why not just do: 为什么不做:
function ceilingAbsolute(inVal, pos){
var digits = Math.pow(10, pos);
return parseInt(inVal / digits) * digits;
}
You can also do: 您也可以这样做:
function absFloor(num, pos) {
num += '';
var len = num.length;
if (pos < len) {
return num.substring(0,len-pos) + ('' + Math.pow(10, pos)).substring(1);
}
}
It can be reduced to two lines, but I think the test is important: 可以减少到两行,但是我认为测试很重要:
function absFloor(num, pos) {
num += '';
return num.substring(0,num.length-pos) + ('' + Math.pow(10, pos)).substring(1);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.