Nov 09 2009

Parse a float number in javascript

Category: JavascriptGiulio Pons @ 10:59 pm

This small code parse a javascript float number to a string. It converts the number to a string and let you decide how many decimals use.

It’s very good to change the value of an input box using onfocus or onblur events, for example, when a user input a price.


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;
}
Share

Related posts:

  1. Get file name in javascript
  2. Decimal Degrees conversion and distance of two points on google map
  3. Ruler for Google Maps v3 to measure distance on map
  4. Set “write here” on input type text?
  5. How to capture enter key pressed in a form (JavaScript)

Tags: , ,

Leave a Reply