Nov 09 2009

Parse a float number in javascript

Category: Java Scriptadmin @ 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/Bookmark

Related posts:

  1. Decimal Degrees conversion and distance of two points on google map
  2. Ruler for Google Maps v3 to measure distance on map
  3. Set “write here” on input type text?
  4. How to capture enter key pressed in a form (JavaScript)
  5. How do I get the value from a combo in Javascript?

Tags: , ,

Leave a Reply