保留两位小数(不够的添0补齐)
function changeTwoDecimal_f(x){
var f_x = parseFloat(x);
if (isNaN(f_x)){return 0;}
var f_x = Math.round(x*100)/100;
var s_x = f_x.toString();
var pos_decimal = s_x.indexOf('.');
if (pos_decimal < 0){pos_decimal = s_x.length;s_x += '.';}
while (s_x.length <= pos_decimal + 2){s_x += '0';}
return s_x;
}
去掉数字(整数、小数)前面多余的零
例如:
-
如果是整数:去掉整数前的零 例如:0098765--->98765
-
如果是小数:去掉小数前的零 例如:000.333--->0.333 再例如000.00--->0.00
function prefix_excpet_0(x){
x = (x+'').replace(/^0+\./g,'0.');
x.match(/^0+[1-9]+/) ? x = x.replace(/^0+/g,'') : x;
x = Number(x) ? x : 0;
return x;
}