Format Numbers/Currency

JavaScript topics covered:

The following methods are supported by JavaScript 1.5 (IE5.5+ and NS6+)

  1. toFixed(x)

    To easily format numbers for currency (8.00) or a specific number of trailing decimals JavaScript 1.5 introduces toFixed(x) where x is the number of trainling decimals. If the number has more trailing decimals than needed, toFixed() will round up. For example:

    a=1.5
    a.toFixed(2)	// 1.50    (trailing zero added)
    a.toFixed(3)	//1.500 	 
    a.toFixed(4)	//1.5000
    
    b=34.76549
    b.toFixed(2)	//34.77  (value is rounded up to 34.77)
    b.toFixed(3)	//34.765 
    b.toFixed(4)	//34.7655 (value is rounded up to 34.7655)
    

    When using toFixed(), the method does not change the value of the variable. See the example below:

    var price = 19.95, qty=3, total
    total = parseFloat(price) * parseInt(qty)
    

    *** Notice that the value of total did not change ***

    Click here to see the demo

  2. toPrecision(x)

    Formats any number so it is of "x" length. Also called significant digits. A decimal point and "0"s are used if needed to create the desired length.

    a=1.5
    a.toFixed(2)	//1.5    
    a.toFixed(3)	//1.50	 (trailing zero added)
    a.toFixed(4)	//1.500
    
    b=34.76549
    b.toFixed(2)	//35  (value is rounded up)
    b.toFixed(3)	//34.8 (value is rounded up)
    b.toFixed(4)	//34.77 (value is rounded up)
    

    When using toFixed(), the method does not change the value of the variable. See the example below:

    var price = 19.95, qty=3, total
    total = parseFloat(price) * parseInt(qty)
    

    *** Notice that the value of total did not change ***

    Click here to see the demo

    Notice what happens when the number on the left of the decimal point is larger than the length you specify

    var a=3476.549
  3. Function for older browsers
  4. What if the browser doesn't support javaScript 1.5? You can write your own function to format a number. First test to see if the browser supports toFixed. If it does then use toFixed, if not use a function called fix(numToFix, decimalPlaces). Below are two examples of functions that format decimal places after the period.

    function fix(numToFix, decimalPlaces){
    if(isNaN(fixNumber)) 	return 0;
    var div = Math.pow(10, decimalPlaces);	//calculate 10 to the power of the number of decimal places needed
    numToFix = Math.round(numToFix * div)/div;	//multiply the original number by 10^x, round, then Divide by 10^x;
    var string = numToFix.toString();	//convert the number to a string variable
    	var parts = string.split(".");	//create an array with the dollars in one part the cents in the other
    	var cents = parts[1];
    	while (cents.length<decimalPlaces)	//add as many zeros as needed
    	     cents += "0";
    	return parts[0]+"."+cents		//return the formatted number
    }
    
    function formatAsMoney(numToFix) {
        numToFix -= 0;
        numToFix = (Math.round(numToFix*100))/100; //multiply the original number by 10^2, round, then Divide by 10^2;
        return (numToFix == Math.floor(numToFix)) ? numToFix + '.00' //no decimal places? add 2 zeros else
                  : ((numToFix*10 == Math.floor(numToFix*10)) ? 	//one decimal place?
                           numToFix + '0' : numToFix);				//add one zero  else number already has 2 decimals
    }
    
    Check to see if the browser supports toFixed(), if not then call function fix passing two parameters
    
    if (total.toFixed) //this will return true or false
    	document.write(total.toFixed(2));
    else
    	document.write(fix(total, 2));
    
    
    
    
    Click here to see the demo