Ken
Ken

Reputation: 1

Decimal places in calculation

I'm not a java expert by any stretch but I'm using the code below to make calculations. The code works well but I need to limit the final total to 2 decimal places as the answer is a figure in £. Example I want £123.45 but I'm getting figures such as £123.456.

This is my code. Need to know where and what to put in this to make 2 decimal places

<script>
    jQuery(document).ready(function ($) {
        var calculate = function () {
            var total = 0.00;
 
            // Check the chosen option of a select menu
            var val1 = $('.iphorm_3_1').val();
            if (val1 == '25 sqft') {
                total += 17.94;
            } else if (val1 == '35 sqft') {
                total += 22.74;
            } else if (val1 == '50 sqft') {
                total += 29.94;
            } else if (val1 == '75 sqft') {
                total += 39.54;
            } else if (val1 == '100 sqft') {
                total += 45.54;
            } else if (val1 == '125 sqft') {
                total += 53.94;
            } else if (val1 == '150 sqft') {
                total += 59.94;
            } else if (val1 == '200 sqft') {
                total += 71.94;
            } 
 
            // A second select menu
            var val2 = $('.iphorm_3_2').val();
            if (val2 == '1 week') {
                total *= 1.00;
            } else if (val2 == '2 weeks') {
                total *= 2.00;
            } else if (val2 == '3 weeks') {
                total *= 3.00;
            } else if (val2 == '4 weeks') {
                total *= 4.00;
            } else if (val2 == '5 weeks') {
                total *= 5.00;
            } else if (val2 == '6 weeks') {
                total *= 6.00;
            } else if (val2 == '7 weeks') {
                total *= 7.00;
            } else if (val2 == '8 weeks') {
                total *= 8.00;
            } else if (val2 == '9 weeks') {
                total *= 9.00;
            } else if (val2 == '10 weeks') {
                total *= 10.00;
            } else if (val2 == '11 weeks') {
                total *= 11.00;
            } else if (val2 == '12 weeks') {
                total *= 12.00;
            }
 
             
            // A third select menu
            var val3 = $('.iphorm_3_5').val();
            if (val3 == 'Immediately') {
                total += total/100*80-total;
            } else if (val3 == 'Within a week') {
                total += 0.00;
            } else if (val3 == 'Within two weeks') {
                total += 0.00;
            } else if (val3 == 'Within a month') {
                total += 0.00;
            }
 
        
 
            // Display the result to the user
            $('#form-total').text('Total: $' + total);
 
            // Set the value of the hidden field
            $('input[name=iphorm_3_8]').val('£' + total);
        };
 
        // Calculate on page load
        calculate();
 
        // Recalculate when these select menus are changed
        $('.iphorm_3_1, .iphorm_3_2, .iphorm_3_5').change(calculate);
 
    });
</script>

Upvotes: 0

Views: 50

Answers (2)

gurvinder372
gurvinder372

Reputation: 68393

Use toFixed(2)

    // Display the result to the user
     $('#form-total').text('Total: $' + total.toFixed(2));

    // Set the value of the hidden field
     $('input[name=iphorm_3_8]').val('£' + total.toFixed(2));

Or you can simply set the value before you show them

    total = total.toFixed(2);
    // Display the result to the user
     $('#form-total').text('Total: $' + total);

    // Set the value of the hidden field
     $('input[name=iphorm_3_8]').val('£' + total);

Upvotes: 0

Ren Camp
Ren Camp

Reputation: 440

You can use

total = Math.round(total * 100) / 100;

Upvotes: 1

Related Questions