Getting Money Format

Hi,

This prints out the like 57 and I would like it to print out like $57.99. I’ve tried many things but I know very little javascript.

Here’s my code:

    <script>
    jQuery(document).ready(function()	
    {jQuery('input[type="checkbox"]').change(function() {
       var total_span = 0;
       
    jQuery('input[type="checkbox"]').each(function() {
        if (jQuery(this).is(':checked')) {
            total_span += parseInt(jQuery(this).prop('value'));
    			
        }
    });

    jQuery("#usertotal").html(total_span);
    });});
    </script>

  <span id="usertotal"> </span>

Thanks

This will return an integer so you won’t get any decimal places. Try using parseFloat(jQuery(this).prop('value')).toFixed(2). That will give you the two decimal places.

1 Like

Thanks that works but it outputs numbers like 05.99 and it stop adding the total of the amounts together and displays them all.

You will find that a more reliable technique is to instead adjust the section that does the output to the screen.

jQuery("#usertotal").html(total_span.toFixed(2));

I’m sorry I made a mistake in my last post about it outputting 05.99. It doesn’t output the .99 part. I added your code and now it correctly adds the totals and doesn’t add the zero at the beginning, but it doesn’t add the values pass the decimal point but just zeros like this: 29.00 when it should be 29.99.

Thank you

You’re still using parseInt, which chops off the decimal values.

Use instead Number to turn the value string to a number.

total_span += Number(jQuery(this).prop('value'));
1 Like

Oh yea, that got it! Than you.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.