Round Number upto 2 decimal places in JavaScript

JavaScript’s Math.round() function doesn’t provide facility to round off a number up to 2 or more decimal places. So, here I’m showing a simple JavaScript code using which you can round off numbers up to 2 decimal places. You can change this for more decimal places.

var retailPrice = 9.99;
var percentage = 0.2525; /* 25.25% */
var decimalPlace = 100; /* 100 for 2 decimal place. 1000 for 3 decimal place.*/
 
var wholeSalePrice = Math.round(percentage * retailPrice * decimalPlace) / decimalPlace;
 
alert(wholeSalePrice); /* This will show 2.52 */


  1. #1 by virander chauhan - February 23rd, 2011 at 08:19

    function fillShenandoah()
    {
    var monthlyPremium = document.weeklyProduction.shenandoahPremium.value
    var months = “12″
    var annualPremium = parseFloat(monthlyPremium * months)

    document.weeklyProduction.shenandoahAnnualPremium. value = annualPremium

    }

(will not be published)

 


Submit Comment
Subscribe to comments feed