Link to home
Start Free TrialLog in
Avatar of Robert Granlund
Robert GranlundFlag for United States of America

asked on

jQuery Math Decimal Points

I have a jQuery function that adds up a total.  The issue is, if the number  has 2 decimal points and the last number is 0, it does not show the Zero.  For eample if the number is 9.30  it only shows 9.3.  How do I force it to show the Zero?

Here is part of my function, I hope it helps.

// START TAXES
 		if( state_a == 'WA') {
 			tax = .0011;
 		} else if ( state_a == 'NJ' ) {
 			tax = .009;
 		} else if ( state_a == 'WV' ) {
 			tax = .0055;
 		} else {
 			tax = 0;
 		}
 		
 		var sub_p_t = parseFloat(final_premium_total) * parseFloat(tax);
 		
 		var round_sub_p_t = Math.ceil(sub_p_t * 100) / 100;
 		
 		var pre_tot = parseFloat(final_premium_total) + parseFloat(sub_p_t);
 		
 		if( state_a == 'WA') {
 			premium_total = Math.ceil(pre_tot * 100) / 100; 
 			$('[name="item_options[0][surcharge]"]').val( round_sub_p_t );
 			$(".tax-display").html('(<strong>Tax Included</strong><span class="sale_price pull-right"><span class="cart_total">$ '+ round_sub_p_t +'</span></span>)');
 		} else if( state_a == 'WV') {
 			premium_total = Math.ceil(pre_tot * 100) / 100; 
 			$('[name="item_options[0][surcharge]"]').val( round_sub_p_t );
 			$(".tax-display").html( '(<strong>Tax Included</strong><span class="sale_price pull-right"><span class="cart_total">$ '+ round_sub_p_t +'</span></span>)');
 		} else if( state_a == 'NJ') {
 			premium_total = Math.ceil(pre_tot * 100) / 100; 
 			$('[name="item_options[0][surcharge]"]').val( round_sub_p_t );	
 			$(".tax-display").html( '(<strong>Tax Included</strong><span class="sale_price pull-right"><span class="cart_total">$ '+ round_sub_p_t +'</span></span>)');		
 		} else {
 			premium_total = final_premium_total;
 		}
 	//  END TAXES 

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If you're fine rounding to two decimal places, you can use toFixed. For example,
premium_total = final_premium_total.toFixed(2)

Open in new window

This will also convert the value to a string. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed for more information.
Looks like hielo beat me to submitting. :)