Paul McFedries' Web Home


JavaScript Programs

Rounding a Value to a Specified Number of Decimals

This page demonstrates a JavaScript function that will take any value and round it to a specified number of decimal places. If the value has fewer decimal places than what you want it rounded to, the decimals are padded with zeroes. Try it out:


Enter a value:
Number of decimals:
Rounded value::

In this form, the button uses the following code:
<INPUT TYPE=BUTTON VALUE="Round It" onClick="TryIt()">

This just runs the TryIt( ) JavaScript function:

<script language="JavaScript" type="text/javascript">
<!--
function TryIt() {
    var frm = document.RounderForm
    frm.rounded.value = round_decimals(frm.val.value, frm.decimals.value)
    }
//-->
</script>

This function, in turn, calls the round_decimals( ) function, which is the meat of the matter. The round_decimals( ) function takes two values as inputs:

    function  round_decimals(original_number, decimals)
Here, original_number is the value that you want rounded, and decimals is the number of decimal places you want it rounded to. In the TryIt( ) function, these values are supplied by the form's two TEXT boxes (named val and decimals).

The round_decimals() function is listed in full below. To use this script, copy everything between (and including) the <SCRIPT> and </SCRIPT> tags, and insert it between your page's </HEAD> and <BODY> tags:

<script language="JavaScript" type="text/javascript">
<!--

/* This script is Copyright (c) Paul McFedries and 
Logophilia Limited (http://www.mcfedries.com/).
Permission is granted to use this script as long as 
this Copyright notice remains in place.*/

function round_decimals(original_number, decimals) {
    var result1 = original_number * Math.pow(10, decimals)
    var result2 = Math.round(result1)
    var result3 = result2 / Math.pow(10, decimals)
    return pad_with_zeros(result3, decimals)
}

function pad_with_zeros(rounded_value, decimal_places) {

    // Convert the number to a string
    var value_string = rounded_value.toString()
    
    // Locate the decimal point
    var decimal_location = value_string.indexOf(".")

    // Is there a decimal point?
    if (decimal_location == -1) {
        
        // If no, then all decimal places will be padded with 0s
        decimal_part_length = 0
        
        // If decimal_places is greater than zero, tack on a decimal point
        value_string += decimal_places > 0 ? "." : ""
    }
    else {

        // If yes, then only the extra decimal places will be padded with 0s
        decimal_part_length = value_string.length - decimal_location - 1
    }
    
    // Calculate the number of decimal places that need to be padded with 0s
    var pad_total = decimal_places - decimal_part_length
    
    if (pad_total > 0) {
        
        // Pad the string with 0s
        for (var counter = 1; counter <= pad_total; counter++) 
            value_string += "0"
        }
    return value_string
}

//-->
</script>

Copyright © 1995-2008 Paul McFedries and Logophilia Limited