/ Published in: JavaScript
converts a number "v" to a float number and formats the string it with "d" decimals, used on input type text onblur="parseFloatString(this.value,2);"
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
function parseFloatString (v,d) { var x = parseFloat( !v ? 0 : v); x = parseFloat( Math.round( x * Math.pow(10,d) ) ) / Math.pow(10,d); y = x + ""; if (y.indexOf(".")==-1) y = x + "."; var a = y.split("."); if (a[1].length<d) for(k=a[1].length;k<d;k++) a[1]+="0"; y = a[0]+"."+a[1]; return y; }
URL: http://www.barattalo.it/2009/11/09/parse-a-float-number-in-javascript/