Wednesday, September 12, 2012

Exponential to Fixed notation of Numeric Value using Javascript


Scenario : We are getting some data from service in xml format and this is having a few numeric values in exponential form (ex; 5.6E5), along with other information. But, we need to display it on the web page in fixed notation (ex: 560000).

Solution : Using javascript method toFixed() we can covert any float or exponential values to some fixed format.

ex:

var num = new Number(5.6E4).toFixed();
var x = document.getElementById("txtAmount");
x.innerHTML=num;

Options :

1. We can specify number of digits after decimal point.
ex:
var num = 5.56789;
var n=num.toFixed(2);
Result : 5.57

2. Convert a number, without keeping any decimals
ex:
var num = 5.56789;
var n=num.toFixed();
Result : 6


No comments:

Post a Comment