Return to Snippet

Revision: 19959
at November 4, 2009 06:54 by ginoplusio


Updated Code
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;
}

Revision: 19958
at November 4, 2009 06:52 by ginoplusio


Initial Code
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 + ".00";
	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;
}

Initial URL
http://www.barattalo.it/2009/11/09/parse-a-float-number-in-javascript/

Initial Description
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);"

Initial Title
Parse a float number to a string with x decimals

Initial Tags
number

Initial Language
JavaScript