0

I'm very new to Javascript so please bear with me.

I have this function that adds up a total. How do I make it so that it shows the nearest two decimal places instead of no decimal places?

function calcProdSubTotal() {

    var prodSubTotal = 0;

    $(".row-total-input").each(function() {

         var valString = $(this).val() || 0;

        prodSubTotal += parseInt(valString);

    });

    $("#product-subtotal").val(CommaFormatted(prodSubTotal));

}

Thank you!

Edit: As requested: commaFormatted:

function CommaFormatted(amount) {

    var delimiter = ","; 
    var i = parseInt(amount);

    if(isNaN(i)) { return ''; }

    i = Math.abs(i);

    var minus = '';
    if (i < 0) { minus = '-'; }

    var n = new String(i);
    var a = [];

    while(n.length > 3)
    {
        var nn = n.substr(n.length-3);
    a.unshift(nn);
    n = n.substr(0,n.length-3);
}

if (n.length > 0) { a.unshift(n); }

n = a.join(delimiter);

amount = "$" + minus + n;

return amount;

}
Tara
  • 251
  • 1
  • 5
  • 17
  • Can you show us the `CommaFormatted` function that you are running it through? – crush Jan 09 '14 at 18:12
  • I've edited my question with CommaFormatted – Tara Jan 09 '14 at 18:13
  • Glad you showed us. It's going to need some changes too. – crush Jan 09 '14 at 18:19
  • I'd like to note that this isn't a script I wrote. This is something I got from css-tricks http://css-tricks.com/multi-product-quantity-based-order-form/ – Tara Jan 09 '14 at 18:26
  • @crush please note the above comment. I've been messing with the script to get it to show the decimals but it won't. Can you provide me with an answer? Thank you! – Tara Jan 09 '14 at 18:33
  • see @plalx's updated answer below. He has provided you with a replacement `commaFormatted` function to use instead. – crush Jan 09 '14 at 18:45
  • @Tara Did you look at my answer? – plalx Jan 09 '14 at 23:22

4 Answers4

1

Well parseInt parses integers, so you are getting rid of any decimals right there. Use parseFloat.

E.g.

parseFloat('10.599').toFixed(2); //10.60

You might also want to change your commaFormatted function to something like:

function commaFormatted(amount) {
    if (!isFinite(amount) || typeof amount !== 'number') return '';

    return '$' + amount.toFixed(2).replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,');
}


commaFormatted(0); //$0.00
commaFormatted(1.59); //$1.59
commaFormatted(999999999.99); //$999,999,999.99
plalx
  • 42,889
  • 6
  • 74
  • 90
  • Her `CommaFormatted` function also does a `parseInt`. It also looks like decimals will screw up the `CommaFormatted` function. – crush Jan 09 '14 at 18:18
  • How can I change CommaFormatted to not chop off any decimals? – Tara Jan 09 '14 at 18:20
  • @Tara Changing that `parseInt` in `CommaFormatted` will help, but then your algorithm for adding the decimals is going to get thrown off. You should use a [regular expression to comma format it](http://stackoverflow.com/questions/2254185/regular-expression-for-formatting-numbers-in-javascript). – crush Jan 09 '14 at 18:23
  • Surely the commas in place of dots are a localization issue, and will be handled automatically by `.toFixed()`? – Blazemonger Jan 09 '14 at 18:24
0

You're looking for toFixed(). It takes one parameter, digits. The parameter is documented as follows:

The number of digits to appear after the decimal point; this may be a value between 0 and 20, inclusive, and implementations may optionally support a larger range of values. If this argument is omitted, it is treated as 0.

Do also note that parseInt() parses integers, truncating any decimals you might have. parseFloat() will preserve decimals as expected.

Kevin Sjöberg
  • 2,261
  • 14
  • 20
  • You're assuming that this `CommaFormatted` function doesn't screw with that. It probably doesn't, so that will probably work fine. – crush Jan 09 '14 at 18:12
0

Use to function toFixed(2)

The 2 is an integer parameter that says use 2 decimal points, assuming your comma formatted code does not turn it into a string. (If it does, fix the decimals BEFORE you run it through the formatting)

$("#product-subtotal").val(CommaFormatted(parseFloat(prodSubTotal).toFixed(2)));

Remember to parseFloat because the val() could be a string!`

Sterling Archer
  • 22,070
  • 18
  • 81
  • 118
0

I solved my problem. I simply changed:

 $("#product-subtotal").val(CommaFormatted(prodSubTotal));

to

  $("#product-subtotal").val(prodSubTotal);

As I stated in the comments, this was not a script I wrote. It is a script Chris Coyier wrote and I was just trying to amend it. I guess I didn't need to use CommaFormatted for my purposes?

Thank you all for your help!

Tara
  • 251
  • 1
  • 5
  • 17