Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old January 23rd, 2005, 07:31 PM
Friend of Wrox
Points: 1,243, Level: 13
Points: 1,243, Level: 13 Points: 1,243, Level: 13 Points: 1,243, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2003
Location: , , United Kingdom.
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default Two decimal places only

Hi,

I can not figure ou what is happening in this code. If I multiply 3.99 * 5 I am getting 19.950000000000003. So I get lots of places after the decimal point. Now if I multiply by 3 I get only two decimal places 3.99 * = 11.97

also, the Total field of my table is not behaving the way I want. Sometimes I get lots of places and sometimes only two decimal places.

Could someone please help me to sort this out. I would like to give the user the choice to choose only two decimal places or three..

Please run the code below to see what the problem is.

The code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
    <head>
        <title></title>
        <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
        <meta name="ProgId" content="VisualStudio.HTML">
        <meta name="Originator" content="Microsoft Visual Studio .NET 7.1">
        <style>
            H1, P {font-family:verdana}
            TH {font-family:verdana; text-align:center}
            TD {font-family:verdana; text-align:left}
            INPUT {font-family:verdana; text-align:right}
        </style>

        <script type="text/javascript">
        <!--
            function ReCalc()
            {
                var f = document.orderform;

                f.intsub1.value = parseInt(f.intqty1.value) * 3.99;
                f.intsub2.value = parseInt(f.intqty2.value) * 15.99;
                f.intsub3.value = parseInt(f.intqty3.value) * 8.57;

                f.intTotal.value = parseFloat(f.intsub1.value) +
                                    parseFloat(f.intsub2.value) +
                                    parseFloat(f.intsub3.value) ;
            }
        -->
        </script>
    </head>
    <body>
        <form method="post" action="AddMultiply.htm" name="orderform">
            <TABLE WIDTH="552" BORDER="1" CELLSPACING="1" CELLPADDING="0">
                <TR>
                    <TH width="54">
                        Quantity</TH>
                    <TH width="101">
                        Product ID</TH>
                    <TH width="106">
                        Description</TH>
                    <TH width="100">
                        Unit Price</TH>
                    <TH width="100">
                        SubTotal</TH>
                </TR>
                <TR>
                    <TD width="54"><input type="text" name="intqty1" size="7" value="5"></TD>
                    <TD width="101">101-abc</TD>
                    <TD width="106">Perfume</TD>
                    <TD width="100">3.99</TD>
                    <TD width="100"><input type="text" name="intsub1" size="12" value=""></TD>
                </TR>
                <TR>
                    <TD width="54"><input type="text" name="intqty2" size="7" value="1"></TD>
                    <TD width="101">234rf</TD>
                    <TD width="106">Photo</TD>
                    <TD width="100">15.99</TD>
                    <TD width="100"><input type="text" name="intsub2" size="12" value=""></TD>
                </TR>
                <TR>
                    <TD width="54"><input type="text" name="intqty3" size="7" value="1"></TD>
                    <TD width="101">f98f</TD>
                    <TD width="106">Jar</TD>
                    <TD width="100">8.57</TD>
                    <TD width="100"><input type="text" name="intsub3" size="12" value=""></TD>
                </TR>
                <tr>
                    <td width="315" colspan="3">
                        <input type="button" value="Update order" name="cmdUpdate" style="text-align:center"
                         onclick="ReCalc()">
                    </td>
                    <td width="100">Total</td>
                    <td width="100"><input type="text" name="intTotal" size="12" value=""></td>
                </tr>
            </TABLE>
        </form>
    </body>
</html>




Reply With Quote
  #2 (permalink)  
Old January 24th, 2005, 12:39 AM
Friend of Wrox
 
Join Date: Dec 2004
Location: Chennai, Tamil nadu, India.
Posts: 307
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Vadivel Send a message via Yahoo to Vadivel
Default

Change the javascript function as follows:

function ReCalc()
{
var f = document.orderform;
f.intsub1.value = Number(parseInt(f.intqty1.value) * 3.99).toFixed(2);
f.intsub2.value = Number(parseInt(f.intqty2.value) * 15.99).toFixed(2);
f.intsub3.value = Number(parseInt(f.intqty3.value) * 8.57).toFixed(2);
f.intTotal.value = Number(parseFloat(f.intsub1.value) + parseFloat(f.intsub2.value) + parseFloat(f.intsub3.value)).toFixed(2);
}

Best Regards
Vadivel

MVP ASP/ASP.NET
http://vadivel.thinkingms.com
Reply With Quote
  #3 (permalink)  
Old January 25th, 2005, 07:56 AM
Friend of Wrox
Points: 1,243, Level: 13
Points: 1,243, Level: 13 Points: 1,243, Level: 13 Points: 1,243, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2003
Location: , , United Kingdom.
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default

Hi Vadivel,

Many thanks for your help. It is working now. I have just one more question:

1 - is it possible to force the number to round up or down ?

suppose the result of the multiplication is 13.55. How can I round it up to 13.6 or round down to 13.5??

Cheers,

Claudio



Reply With Quote
  #4 (permalink)  
Old January 25th, 2005, 02:31 PM
Friend of Wrox
 
Join Date: Dec 2004
Location: Chennai, Tamil nadu, India.
Posts: 307
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Vadivel Send a message via Yahoo to Vadivel
Default

For rounding off numbers make use of Math.round and Math.pow .. .check the below sample:

<html>
<head>
<script language="javascript">
function fnRound()
{
   var formField = document.frmTest.txtValue;
   var roundedValue = Math.round(formField.value*Math.pow(10,1))/Math.pow(10,1);
   formField.value = roundedValue;
}
</script>
</head>
<body>
<form name="frmTest">
   <input type="text" name="txtValue">
   <input type="button" value="Round" onClick="fnRound();">
</form>
</body>
</html>

Best Regards
Vadivel

MVP ASP/ASP.NET
http://vadivel.thinkingms.com
Reply With Quote
  #5 (permalink)  
Old January 28th, 2005, 09:55 AM
Friend of Wrox
Points: 1,243, Level: 13
Points: 1,243, Level: 13 Points: 1,243, Level: 13 Points: 1,243, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2003
Location: , , United Kingdom.
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default

Hi Vadivel,

Thanks ever so much for your help and sharing your knowledge with us in the forum. I have just tested the sample code you provided and it works fine.

Cheers,

Claudio
:)


Reply With Quote
  #6 (permalink)  
Old February 2nd, 2005, 12:35 AM
Friend of Wrox
 
Join Date: Dec 2004
Location: Chennai, Tamil nadu, India.
Posts: 307
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Vadivel Send a message via Yahoo to Vadivel
Default

Cool .. glad to know that my code was of help to you :)

Best Regards
Vadivel

MVP ASP/ASP.NET
http://vadivel.thinkingms.com
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
reg. exp. for up to 2 decimal places everest Javascript How-To 1 February 11th, 2008 04:51 PM
Number with 2 decimal places only pallone SQL Server 2000 4 July 3rd, 2007 07:29 AM
Display Decimal Places rsm42 ASP.NET 1.0 and 1.1 Basics 3 January 7th, 2007 10:46 AM
Round to 2 decimal places adman JSP Basics 0 June 30th, 2005 07:24 AM
output only 2 decimal places Toka1 Javascript How-To 2 February 20th, 2004 11:44 AM



All times are GMT -4. The time now is 01:28 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.