Revision: 13161
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at April 12, 2009 15:04 by amischol
Initial Code
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; };
Initial URL
Initial Description
This snippet is a reemplacement for the parseFloat function of the Javascript language. This new function gets one or two parameters if only one parameter is defined the function is like the original parseFloat. If there are two parameter passed to the function, the first parameter must be a string or number and the second parameter must be the number of decimals we want to get, if the last decimal is greater or equal than five the last decimal is incremented by one.
Initial Title
ParseFloat reemplacement with a limit of decimals to show
Initial Tags
javascript
Initial Language
JavaScript