Click here to Skip to main content
15,891,777 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
<script language="javascript" type="text/javascript">
function mul() {

        var net_wt = document.getElementById('<%= txtPre_NetWt.ClientID %>');
        var    ip1 = document.getElementById('<%= txtPrep_RecIp.ClientID %>');
        var     Wt = document.getElementById('<%= txtPre_Wt.ClientID %>');
        var     z1 = 0, z2 = 0 ;


        if (net_wt.value != "") z1 = net_wt.value;
        if (ip1.value !="") z2  =ip1.value;
                 Wt.value =parseInt(z1)* parseInt(z2)* 0.01;
            

    }
</script>


Above javascript multilication, i want to round up 3 decimal places for wt.value... for example Wt=78.1159round for 3 decimal
like Wt=78.116

How can do this piz help any one... i tried some codes , they dont do accurate calculation..

so please help me....
Posted

Hi,

Have a look at JavaScripts toFixed Method[^].

In you example, it would be used like:
JavaScript
function mul() {
 
        var net_wt = document.getElementById('<%= txtPre_NetWt.ClientID %>');
        var    ip1 = document.getElementById('<%= txtPrep_RecIp.ClientID %>');
        var     Wt = document.getElementById('<%= txtPre_Wt.ClientID %>');
        var     z1 = 0, z2 = 0 ;
 

        if (net_wt.value != "") z1 = net_wt.value;
        if (ip1.value !="") z2  =ip1.value;
                 Wt.value =(parseInt(z1)* parseInt(z2)* 0.01).toFixed(3);
            
 
    }


... hope it helps.
 
Share this answer
 
Hi,

1. You can use Math.Round function to round up your values.
2. parseFloat("123.456").toFixed(3);

For reference please follow the following link.

1. round-to-at-most-2-decimal-places

2. how-to-round-up-a-number-in-javascript

3. Math.Round()

Hope you can achieve your requirement now.

Thanks & Regards
Sisir patro
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900