[javascript] Currency Formatting in JavaScript

Possible Duplicate:
How can I format numbers as money in JavaScript?

I have a form with some simple JavaScript to perform an instant calculation. My problem is that I'm struggling to format it to display correctly with commas and 2 decimal places.

Any help would be very much appreciated. Thank you.

    <p>
    <label>My Daily Rate is:</label><br />
<input class="poundsBox" name="shares" id="shares" type="text" /><br />
<br />
<strong>Your Gross Contract Take Home:</strong></p>
<p><span class="result">&pound; <span id="result"></span></span></p>
The above illustration is provided for guidance only. Please complete the request form below for a detailed personal illustration.

<script type="text/javascript">
$("#shares").keyup(function() {
   var val = parseFloat($(this).val());
   // If val is a good float, multiply by 260, else show an error
   val = (val ? val * 260 * 0.88 : "Invalid number");
   $("#result").text(val);
})
</script>

This question is related to javascript jquery

The answer is


You could use toPrecision() and toFixed() methods of Number type. Check this link How can I format numbers as money in JavaScript?