41

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>
Viktor Borítás
  • 135
  • 2
  • 11
kevg
  • 413
  • 1
  • 4
  • 5
  • 2
    It's a bad idea to use fractional floating-point numbers to represent currency values. – cdhowie Jan 22 '13 at 20:30
  • @cdhowie So you expect the user to input an integer, representing cents, not dollars (pounds)? How else do you expect this to be done? – Ian Jan 22 '13 at 20:52
  • 1
    @Ian One would have to write a function that parses `"123.45"` into `12345`, for example. This is not terribly difficult to do. – cdhowie Jan 22 '13 at 20:58
  • @cdhowie So what, `return parseInt(parseFloat(input)*100);`? – Ian Jan 22 '13 at 21:34
  • @Ian No, that's susceptible to the same problem. The parsing has to be done without `parseFloat()`. – cdhowie Jan 22 '13 at 22:14
  • @cdhowie That was my point. So how is that done? Just by moving the decimal point (in the input string) two places to the right (and/or filling in empty trailing 0s? And then use `parseInt` on it and multiply each of the other numbers by 100 before doing the math? So they're all 100x larger and integers? Then get the result by putting a decimal a certain amount of places in from the right? I'm just babbling, but I get the idea. I want to figure this out now :) – Ian Jan 22 '13 at 22:50
  • You should give a look that this library http://josscrowcroft.github.com/accounting.js/ – yvan Jan 22 '13 at 20:30
  • @Ian Sure, that would be one way to do it. You could also write your own parser function that evaluates each character, similar to how `parseInt()` operates internally. – cdhowie Jan 23 '13 at 15:05

2 Answers2

121

You can use standard JS toFixed method

var num = 5.56789;
var n=num.toFixed(2);

//5.57

In order to add commas (to separate 1000's) you can add regexp as follows (where num is a number):

num.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,")

//100000 => 100,000
//8000 => 8,000
//1000000 => 1,000,000

Complete example:

var value = 1250.223;
var num = '$' + value.toFixed(2).replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");

//document.write(num) would write value as follows: $1,250.22

Separation character depends on country and locale. For some countries it may need to be .

Tom
  • 26,212
  • 21
  • 100
  • 111
2

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

Community
  • 1
  • 1
Harish
  • 270
  • 3
  • 11