# Javascript Decimals and Significant digits

One of the most common tasks is to format a number for currency display- an integer followed by two decimals.
You may be tempted to use number rounding to first shift the number’s decimal places (via multiplication), round it, then shift the decimal back (via division) to pound the number into your hard earned dollar, though that won’t work in many cases.
For example, consider the number 120. Number rounding certainly won’t get you to 120.00.

To easily format numbers for a specific number of trailing decimals or total digits (aka padding), JavaScript 1.5 introduces the below two methods:

Methods                      Description

Number.toFixed(x)              Formats any number for “x” number of trailing decimals.
The number is rounded up, and “0”s are used after the decimal point
if needed to create the desired decimal length.

Number.toPrecision(x)        Formats any number so it is of “x” length. Also called significant digits.
A decimal point and “0”s are used if needed to create the desired length

Number.toFixed()

The best way to see all the subtleties of toFixed() is to see it in action:

```var dummyNumber=2489.8237
dummyNumber.toFixed(3) //returns 2489.824 (round up)
dummyNumber.toFixed(2) //returns 2489.82

Displaying any number in currency format can’t get any easier!

Number.toPrecision()

To toPrecision() now:

```var dummyNumber=123.45
dummyNumber.toPrecision(4) //returns 123.5 (round up)
dummyNumber.toPrecision(2) //returns 1.2e+2 (you figure it out!)```

toPrecision() is useful if your number must be of a certain length.

Browser considerations

Now, as noted, our two heros above are JavaScript 1.5 methods. What this means is that they’ll only work in IE5.5+ and NS6+. The issue of legacy browsers not performing the desired formatting operation not withstanding, how do you ensure that these two methods at least degrade well? Well, by using method detection in your code. For example:

var dummyNumber=2489.8237
if (dummyNumber.toFixed) //if browser supports toFixed() method
dummyNumber.toFixed(2)

Taken From JavaScript Kit

# Timing Events in JavaScript : JavaScript Timers

With JavaScript, it is possible to execute some code after a specified time interval.
This is called timing events.

So it is like: if you want to execute a code 2 minutes later from the page load , you can do that with a simple trick and here the trick is given to us by “God of Client Side Programming” JavaScript.

## JavaScript Timing Events

It’s very easy to time events in JavaScript. The two key methods that are used are:

• setTimeout() – executes a code some time in the future
• clearTimeout() – cancels the setTimeout()

Note: The setTimeout() and clearTimeout() are both methods of the HTML DOM Window object.

## setTimeout()

### Syntax

 `var t = setTimeout("javascript statement",milliseconds);`

The setTimeout() method returns a value – In the statement above, the value is stored in a variable called t. If you want to cancel this setTimeout(), you can refer to it using the variable name.

The first parameter of setTimeout() is a string that contains a JavaScript statement. This statement could be a statement like “alert(‘5 seconds!’)” or a call to a function, like “alertMsg()”.

The second parameter indicates how many milliseconds from now you want to execute the first parameter.

Note: There are 1000 milliseconds in one second.

### Example

When the button is clicked in the example below, an alert box will be displayed after 5 seconds.

 ```
```

### Example – Infinite Loop

To get a timer to work in an infinite loop, we must write a function that calls itself. In the example below, when the button is clicked, the input field will start to count (for ever), starting at 0:

 ```
```

## clearTimeout()

### Syntax

 `clearTimeout(setTimeout_variable)`

### Example

The example below is the same as the “Infinite Loop” example above. The only difference is that we have now added a “Stop Count!” button that stops the timer:

 ```
```

Taken from W3SCHOOLS Site : http://www.w3schools.com/js/js_timing.asp
This small article helps me great , so I think somebody will find it useful.