JavaScript ParseFloat重新植入,显示小数限制
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaScript ParseFloat重新植入,显示小数限制相关的知识,希望对你有一定的参考价值。
var proxyFloat = window.parseFloat;
window.parseFloat = function(number,nDecimals){
var amischol = true;
var stringNumber = number.toString(), lastDec, posFinal, numberMore, result, decimalType, decimals;
var decimalTypes = [".",","];
for(var i = 0; i < decimalTypes.length; i++){
pos = stringNumber.indexOf(decimalTypes[i]);
if(pos != -1){
decimalType = decimalTypes[i];
break;
}
}
decimals = (stringNumber.length-1)-pos;
if(typeof nDecimals != "undefined"){
decimals = nDecimals;
}
posFinal = pos+(decimals+1);
if(pos != -1){
lastDec = stringNumber.substr(posFinal,1);
stringNumber = stringNumber.substr(0,posFinal);
if(lastDec >= 5){
numberMore = stringNumber.substr(stringNumber.length-1,1);
if(numberMore == decimalType){
stringNumber = (stringNumber.substr(0,stringNumber.length-1)*1)+1;
}else{
stringNumber = stringNumber.substr(0,stringNumber.length-1)+((numberMore*1)+1);
}
}
}
result = proxyFloat(stringNumber);
return result;
};
以上是关于JavaScript ParseFloat重新植入,显示小数限制的主要内容,如果未能解决你的问题,请参考以下文章
JavaScript - parseFloat 强制 2 位小数作为 NUMBER
哪个javascript函数像parsefloat一样将语言环境作为参数?
Javascript数值转换(Number,parseInt,parseFloat)
parseFloat的取值
JavaScript日常会跳的坑系列
parseFloat()