[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Number formatting problems

I have a modified javascript that is a form which calculates order totals,
quantity discounts, etc. I'm not a javascript person, and know just enough
to modify it to make it work for me. The numbers (such as 14.95) return as
14.94999999999999929. Is there anyway to round it up and truncate it to two
decimal places? I've attached the file, and would appreciate examples based
upon it if possible. When using the HTML file, type 119 under ITEM to see
the problem. You'll notice that under PRICE, AMOUNT, TOTAL, DISCOUNT and
GRAND TOTAL the number is a bit lengthy!


<title>order form</title>
<!-- Start hide

/* Copyright 1996 NetXperts. All rights reserved.

   Derived from works Copyright (C)1996 SDAS, Inc. DBA GeneralNET
   All Rights Reserved.


function contents (item, desc, price)
   if (item.value=="")
   if (item.value=="100")
       {desc.value="AFRI Brochure - Free";
   if (item.value=="104")
       {desc.value="Fund Raising Ideas";
   if (item.value=="105")
       {desc.value="In Your Honor";
   if (item.value=="119")
       {desc.value="Kentucky Derby Party";

   alert("The number you have entered is not valid. Enter the three-digit
Booklet Number (ex. 119). There is no need to enter the # sign.");
   item.value = "";

function checkit(input)
  for (var k = 0; k < input.length; k++)
      {var ch = input.substring(k, k + 1);
      if ((ch > "0" ||  ch=="0") &&  (ch < "9" || ch=="9"))
           return false;
    return true;

function compute(qty, sum, price, input)
    if (qty.value=="" && price.value!="")
       {alert("Please enter the number you wish to order in the Quantity
    if (qty.value=="")
    var str = qty.value;
    if (checkit(str))
      {sum.value = price.value * qty.value;
       if (input.total.value == null || input.total.value == "")
           input.total.value = 1 * 0;
       input.total.value =  (sum.value * 1.00) + (input.total.value * 1.00);
      {alert("The number you entered in the Quantity field (" + str + ") is
incorrect. Please enter the number you wish to order in the Quantity
       qty.value = "";

function getdisc(input, val)
   if (val == "10")
      {input.discval.value = (input.total.value * .10);
   else if (val == "20")
      {input.discval.value = (input.total.value * .20);

      {input.discval.value = 0.00;}
   input.grand.value = ((input.total.value * 1.00 + 2.00) -
(input.discval.value * 1.00));

// generic positive number decimal formatting function
function format (expr, decplaces) {
        // raise incoming value by power of 10 times the
        // number of decimal places; round to an integer; convert to string
        var str = "" + Math.round (eval(expr) * Math.pow(10,decplaces))
        // pad small value strings with zeros to the left of rounded number
        while (str.length <= decplaces) {
                str = "0" + str
        // establish location of decimal point
        var decpoint = str.length - decplaces
        // assemble final result from: (a) the string up to the position of
        // the decimal point; (b) the decimal point; and (c) the balance
        // of the string.  Return finished product.
        return str.substring(0,decpoint) + "." +

// turn incoming expression into a dollar value
function dollarize (grand) {
        return "$" + format(expr,2)

function clear(input)
   input.total.value = 0;
// Stop Hide -->

<CENTER><FONT SIZE=6><B>Here's How to Order:</B><P></FONT></CENTER>

<FONT SIZE=4>This is an automated order form. It will calculate your totals
for you. Simply enter in the booklet number and quantity, then indicate
which discount applies. Your total will be computed (including a $2.00
shipping and handling charge). Click the submit button, send a check or
money order made payable to AFRI to the adress below, and you'll receive
your order promptly. Thank you!</FONT><P>
<FORM METHOD=POST ACTION="/cgi-bin/formmail.pl">
<input type=hidden name="recipient" value="chris@netxperts.com">
<input type=hidden name="subject" value="Order Form">
<input type=hidden name="redirect"

<CENTER><table width=60% border=5 cellpadding=3 cellspacing=2>

<th colspan=5 align=center><font size=5 color="#1100BC"><BR>
American Fund Raising Institute&reg; Order Form<P>
<td colspan=2 align=left><PRE><FONT SIZE=4>
         Name:</FONT> <INPUT TYPE="text" NAME="realname" SIZE=30 >
<FONT SIZE=4>      Address:</FONT> <INPUT TYPE="text" NAME="Address" SIZE=30 >
<FONT SIZE=4>         City:</FONT> <INPUT TYPE="text" NAME="City" SIZE=30 >
<FONT SIZE=4>        State:</FONT> <INPUT TYPE="text" NAME="State" SIZE=2
><FONT SIZE=4>  Zip:</FONT> <INPUT TYPE="text" NAME="Zip" SIZE=10 >

<td colspan=3 align=right><FONT SIZE=4>
    Email:</FONT> <INPUT TYPE="text" NAME="username" SIZE=20 >
<FONT SIZE=4>Telephone:</FONT> <INPUT TYPE="text" NAME="Phone" SIZE=15 >
<FONT SIZE=4>      Fax:</FONT> <INPUT TYPE="text" NAME="Fax" SIZE=15>




<td align=center><font size=4 color="#1100BC"><B>ITEM</B></font></td>
<td align=center><font size=4 color="#1100BC"><B>DESCRIPTION</B></font></td>
<td align=center><font size=4 color="#1100BC"><B>PRICE</B></font></td>
<td align=center><font size=4 color="#1100BC"><B>QTY</B></font></td>
<td align=center><font size=4 color="#1100BC"><B>AMOUNT</B></font></td>
<td><input name="i1" size=10 onchange="contents(this, this.form.d1,
<td><input name="d1" size=40></td>
<td><input name="p1" size=5 value=""></td>
<td><input name="q1" size=5 value="" onchange="compute(this, this.form.s1,
this.form.p1, this.form)"></td>
<td><input name="s1" size=7 value=""></td>
<td><input name="i2" size=10 onchange="contents(this, this.form.d2,
<td><input name="d2" size=40></td>
<td><input name="p2" size=5></td>
<td><input name="q2" size=5 onchange="compute(this, this.form.s2,
this.form.p2, this.form)"></td>
<td><input name="s2" size=7></td>
<td><input name="i3" size=10 onchange="contents(this, this.form.d3,
<td><input name="d3" size=40></td>
<td><input name="p3" size=5></td>
<td><input name="q3" size=5 onchange="compute(this, this.form.s3,
this.form.p3, this.form)"></td>
<td><input name="s3" size=7></td>
<td><input name="i4" size=10 onchange="contents(this, this.form.d4,
<td><input name="d4" size=40></td>
<td><input name="p4" size=5></td>
<td><input name="q4" size=5 onchange="compute(this, this.form.s4,
this.form.p4, this.form)"></td>
<td><input name="s4" size=7></td>
<td><input name="i5" size=10 onchange="contents(this, this.form.d5,
<td><input name="d5" size=40></td>
<td><input name="p5" size=5></td>
<td><input name="q5" size=5 onchange="compute(this, this.form.s5,
this.form.p5, this.form)"></td>
<td><input name="s5" size=7></td>
<td colspan=4 align=right><font size=4><B>TOTAL</B></font></td>
<td><input name="total" size=10></td>
<td colspan=2 align=right><font size=3><B>Order Discouts</B>: Click the
appropriate button to the left:<BR>
1-2 booklets, no discount. 3 to 9 booklets receive a 10% discount, 10 or
more booklets receive a 20% discount.</font></td>
<td colspan=2 align=right>
None <input type="radio" name="disc" value="none"
onclick="getdisc(this.form, 'none')"><BR>
3-9 = 10% off <input type="radio" name="disc" value="10"
onclick="getdisc(this.form, '10')"><BR>
10+ = 20% off <input type="radio" name="disc" value="20"
onclick="getdisc(this.form, '20')"></td>
<td><input name="discval" size=5></td>
<td colspan=4 align=right><font size=3><B>Shipping and Handling</B></font></td>
<td><input name="shipping" size=10 value="2.00"></td>
<td colspan=4 align=right><font size=4 color="#1100BC"><B>Grand
<td><input name="grand" size=10></td>
<td colspan=5 align=center><FONT SIZE=4><B>Payment Method</B></FONT><BR>
<PRE><input type="radio" name="Payment" value="Check/MO"> Check or Money
Order   <input type="radio" name="Payment" value="COD"> COD (US
<td colspan=5 align=center>
<input type=submit value="SEND ORDER"> <input type=reset value="RESET FORM"


Chris Brewer      chris@netxperts.com
NetXperts: Web Page Design
16731 E. Iliff, Suite 214   Aurora CO  80013

303-743-9801   Fax 303-369-5296

World-class design for a world-wide market(tm)