Forum Moderators: open

Message Too Old, No Replies

Round decimals to 2 places

         

atjhza

10:21 pm on Oct 8, 2008 (gmt 0)

10+ Year Member



Hi,

I am very new at all this and am trying to create a small site for my wifes business.

I have altered a script I found but cannot figure out how to round the results to 2 decimal places (including a 0 - i.e 2.50)

Below is the script and any comments/help would be greatly appreciated as life around here is getting a little uncomfortable!

<html>
<head>
<title>Premier Ironing Service</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#FFFFFF" text="#000000">
<table width="56%" border="1" align="center">
<tr>
<td colspan="4"><img src="images/top.jpg" width="750" height="84"></td>
</tr>
<tr>
<th height="19" width="186"><font face="Verdana, Arial, Helvetica, sans-serif" size="1"><a href="index.htm"><font color="#000000">Home</font></a></font></th>
<th height="19" width="186"><font face="Verdana, Arial, Helvetica, sans-serif" size="1"><a href="price.htm"><font color="#000000">Price
List &amp; Calculator</font></a></font></th>
<th height="19" width="186"><font face="Verdana, Arial, Helvetica, sans-serif" size="1"><a href="collection.htm"><font color="#000000">Collection
&amp; Delivery</font></a></font></th>
<th height="19" width="186"><font face="Verdana, Arial, Helvetica, sans-serif" size="1"><a href="terms.htm"><font color="#000000">Terms
&amp; Conditions</font></a></font></th>
</tr>
<tr>
<td colspan="4">
<blockquote>
<blockquote>
<p>
<p><font face="Verdana, Arial, Helvetica, sans-serif"><br>
<br>
<script language="JavaScript">

<!--

function CalculateTotals(){

f=document.orderform;

f.total1.value=parseFloat(f.qty1.value)*("1.10");

f.total2.value=parseFloat(f.qty2.value)*1.00;

f.total3.value=parseFloat(f.qty3.value)*1.50;

f.total4.value=parseFloat(f.qty4.value)*0.5;

f.total5.value=parseFloat(f.qty5.value)*2.00;

f.total6.value=parseFloat(f.qty6.value)*1.00;

f.total7.value=parseFloat(f.qty7.value)*2.50;

f.total8.value=parseFloat(f.qty8.value)*2.00;

f.total9.value=parseFloat(f.qty9.value)*3.00;

f.total10.value=parseFloat(f.qty10.value)*2.50;

f.total11.value=parseFloat(f.qty11.value)*3.50;

f.total12.value=parseFloat(f.qty12.value)*3.00;

f.total13.value=parseFloat(f.qty13.value)*1.00;

f.total14.value=parseFloat(f.qty14.value)*0.80;

f.total15.value=parseFloat(f.qty15.value)*0.90;

f.grandtotal.value=parseFloat(f.total1.value)

+parseFloat(f.total2.value)

+parseFloat(f.total3.value)

+parseFloat(f.total4.value)

+parseFloat(f.total5.value)

+parseFloat(f.total6.value)

+parseFloat(f.total7.value)

+parseFloat(f.total8.value)

+parseFloat(f.total9.value)

+parseFloat(f.total10.value)

+parseFloat(f.total11.value)

+parseFloat(f.total12.value)

+parseFloat(f.total13.value)

+parseFloat(f.total14.value)

+parseFloat(f.total15.value);}

//-->

</script>
<b>Price List &amp; Calculator<br>
</b> </font></p>
<p><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Indicate
how many of each item you wish ironed in the "Qty" column. The total
amount of your order will be calculated automatically.</font></p>
<font face="Verdana, Arial, Helvetica, sans-serif">
<form name="orderform" method="post" action="/htbin/generic">
<table border="3" align="center">
<tr>
<th><font size="2">Qty</font></th>
<th><font size="2">Item</font></th>
<th>
<p><font size="2">Price</font></p>
</th>
<th><font size="2">Total &pound;</font></th>
</tr>
<tr>
<td> <font size="2">
<input name="qty1" size="3" OnBlur="CalculateTotals()" />
</font></td>
<td><font size="2">Shirts / Blouses</font></td>
<td align="right"><font size="2">£1.10</font></td>
<td> <font size="2">
<input name="total1" size="7"

OnFocus="document.orderform.qty2.select();

document.orderform.qty2.focus();" />
</font></td>
</tr>
<tr>
<td> <font size="2">
<input name="qty2" size="3" OnBlur="CalculateTotals()" />
</font></td>
<td><font size="2">Tops/T-Shirts</font></td>
<td align="right"><font size="2">£1.00</font></td>
<td> <font size="2">
<input name="total2" size="7"

OnFocus="document.orderform.qty3.select();

document.orderform.qty3.focus();" />
</font></td>
</tr>
<tr>
<td> <font size="2">
<input name="qty3" size="3" OnBlur="CalculateTotals()" />
</font></td>
<td><font size="2">Trousers/ Jeans / Dresses</font></td>
<td align="right"><font size="2">£1.50</font></td>
<td> <font size="2">
<input name="total3" size="7"

OnFocus="document.orderform.qty4.select();

document.orderform.qty4.focus();" />
</font></td>
</tr>
<tr>
<td> <font size="2">
<input name="qty4" size="3" OnBlur="CalculateTotals()" />
</font></td>
<td><font size="2">Vests / Underwear / Socks</font></td>
<td align="right"><font size="2">£0.50</font></td>
<td> <font size="2">
<input name="total4" size="7"

OnFocus="document.orderform.qty5.select();

document.orderform.qty5.focus();" />
</font></td>
</tr>
<tr>
<td> <font size="2">
<input name="qty5" size="3" OnBlur="CalculateTotals()" />
</font></td>
<td><font size="2">Table Cloths</font></td>
<td align="right"><font size="2">£2.00</font></td>
<td> <font size="2">
<input name="total5" size="7"

OnFocus="document.orderform.qty6.select();

document.orderform.qty6.focus();" />
</font></td>
</tr>
<tr>
<td> <font size="2">
<input name="qty6" size="3" OnBlur="CalculateTotals()" />
</font></td>
<td><font size="2">Napkins</font></td>
<td align="right"><font size="2">£1.00</font></td>
<td> <font size="2">
<input name="total6" size="7"

OnFocus="document.orderform.qty7.select();

document.orderform.qty7.focus();" />
</font></td>
</tr>
<tr>
<td> <font size="2">
<input name="qty7" size="3" OnBlur="CalculateTotals()" />
</font></td>
<td><font size="2">Singke Duvet Cover</font></td>
<td align="right"><font size="2">£2.50</font></td>
<td> <font size="2">
<input name="total7" size="7"

OnFocus="document.orderform.qty8.select();

document.orderform.qty8.focus();" />
</font></td>
</tr>
<tr>
<td> <font size="2">
<input name="qty8" size="3" OnBlur="CalculateTotals()" />
</font></td>
<td><font size="2">Single Sheet Fitted / Flat</font></td>
<td align="right"><font size="2">£2.00</font></td>
<td> <font size="2">
<input name="total8" size="7"

OnFocus="document.orderform.qty9.select();

document.orderform.qty9.focus();" />
</font></td>
</tr>
<tr>
<td> <font size="2">
<input name="qty9" size="3" OnBlur="CalculateTotals()" />
</font></td>
<td><font size="2">Double Duvet Cover</font></td>
<td align="right"><font size="2">£3.00</font></td>
<td> <font size="2">
<input name="total9" size="7"

OnFocus="document.orderform.qty10.select();

document.orderform.qty10.focus();" />
</font></td>
</tr>
<tr>
<td> <font size="2">
<input name="qty10" size="3" OnBlur="CalculateTotals()" />
</font></td>
<td><font size="2">Double Sheet Fitted / Flat</font></td>
<td align="right"><font size="2">£2.50</font></td>
<td> <font size="2">
<input name="total10" size="7"

OnFocus="document.orderform.qty11.select();

document.orderform.qty11.focus();" />
</font></td>
</tr>
<tr>
<td> <font size="2">
<input name="qty11" size="3" OnBlur="CalculateTotals()" />
</font></td>
<td><font size="2">Kingsize Duvet Cover</font></td>
<td align="right"><font size="2">£3.50</font></td>
<td> <font size="2">
<input name="total11" size="7"

OnFocus="document.orderform.qty12.select();

document.orderform.qty12.focus();" />
</font></td>
</tr>
<tr>
<td> <font size="2">
<input name="qty12" size="3" OnBlur="CalculateTotals()" />
</font></td>
<td><font size="2">Kingsize Sheet Fitted / Flat</font></td>
<td align="right"><font size="2">£3.00</font></td>
<td> <font size="2">
<input name="total12" size="7"

OnFocus="document.orderform.qty13.select();

document.orderform.qty13.focus();" />
</font></td>
</tr>
<tr>
<td> <font size="2">
<input name="qty13" size="3" OnBlur="CalculateTotals()" />
</font></td>
<td><font size="2">Pillowcases</font></td>
<td align="right"><font size="2">£1.00</font></td>
<td> <font size="2">
<input name="total13" size="7"

OnFocus="document.orderform.qty14.select();

document.orderform.qty14.focus();" />
</font></td>
</tr>
<tr>
<td> <font size="2">
<input name="qty14" size="3" OnBlur="CalculateTotals()" />
</font></td>
<td><font size="2">Childrens.........Ages 0 to 5</font></td>
<td align="right"><font size="2">£0.80</font></td>
<td> <font size="2">
<input name="total14" size="7"

OnFocus="document.orderform.qty15.select();

document.orderform.qty15.focus();" />
</font></td>
</tr>
<tr>
<td> <font size="2">
<input name="qty15" size="3" OnBlur="CalculateTotals()" />
</font></td>
<td><font size="2">......................Ages 6 to 14</font></td>
<td align="right"><font size="2">£0.90</font></td>
<td> <font size="2">
<input name="total15" size="7"

OnFocus="document.orderform.qty16.select();

document.orderform.qty16.focus();" />
</font></td>
</tr>
<tr>
<td><font size="2"></font></td>
<td><font size="2"></font></td>
<td align="right"><b><font size="2">TOTAL:</font></b></td>
<td> <font size="2">
<input name="grandtotal" size="7"

OnFocus="document.orderform.qty1.select();

document.orderform.qty1.focus();" />
</font></td>
</tr>
</table>
</form>
</font>
<p><font face="Verdana, Arial, Helvetica, sans-serif">
<script language="JavaScript">

<!--

f=document.orderform;

f.qty1.value=0; f.qty2.value=0;

f.qty3.value=0; f.qty4.value=0;

f.qty5.value=0; f.qty6.value=0;

f.qty7.value=0; f.qty8.value=0;

f.qty9.value=0; f.qty10.value=0;

f.qty11.value=0; f.qty12.value=0;

f.qty13.value=0; f.qty14.value=0;

f.qty15.value=0;

f.total1.value=0; f.total2.value=0;

f.total3.value=0; f.total4.value=0;

f.total5.value=0; f.total6.value=0;

f.total7.value=0; f.total8.value=0;

f.total9.value=0; f.total10.value=0;

f.total11.value=0; f.total12.value=0;

f.total13.value=0; f.total14.value=0;

f.total15.value=0

f.grandtotal.value=0.00;

//-->

</script>
</font></p> <font face="Verdana, Arial, Helvetica, sans-serif" size="2">Any
additional information or special requests, please write in the information
box below</font><p></p>
<form name="form1" method="post" action="">
<textarea name="textfield" rows="4" cols="70"></textarea>
</form>
<p align="center"><font size="2"><b><font face="Verdana, Arial, Helvetica, sans-serif" size="4">Please
print this form and hand it to our driver when your items are collected</font></b></font></p>
</blockquote>
</blockquote>
</td>
</tr>
<tr valign="baseline">
<td colspan="4">
<h6 align="center"><font size="1" face="Verdana, Arial, Helvetica, sans-serif">COPYRIGHT
2008</font></h6>
</td>
</tr>
</table>
<p>&nbsp;</p>
</body>
</html>

Thanks.

drooh

11:25 pm on Oct 8, 2008 (gmt 0)

10+ Year Member



you could try using php to do this, number format or money format

httpwebwitch

1:43 am on Oct 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



not the most elegant function, but it does the job:

function CurrencyFormatted(amount){
var i = parseFloat(amount);
if(isNaN(i)) { i = 0.00; }
var minus = (i < 0)?'-':'';
i = Math.abs(i);
i = parseInt((i + .005) * 100);
i = i / 100;
s = new String(i);
if(s.indexOf('.') < 0) { s += '.00'; }
if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
s = minus + s;
return s;
}

source [web-source.net]

Dabrowski

11:07 am on Oct 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Whoa! That really is not elegant!

ok, here's my take:

To do a simple 2dp round, you can't. But you can improvise by shifting the decimal point around.

function round2dp( num) { 
return Math.round( num *100) /100;
}

Now a currency formatting function, but much simpler!

function currencyformat( price) { 
var num = round2dp( price);
var major = Math.abs( num);
var minor = (num -major) *100;
var formatted = "£"+ major +".";

formatted += ( minor <10? "0" : "");
formatted += minor;
formatted += ( !( minor %10)? "0": "");

return formatted;
}

Not tested that but it should work.

httpwebwitch

1:42 pm on Oct 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



simpler still:

function cround(floatnum,dec){
var r = (Math.round(floatnum * Math.pow(10,dec))/Math.pow(10,dec)).toString();
var p = r.lastIndexOf('.');
if(p==-1){r=r+'.'}
k = dec - (p==-1?0:(r.substring(p).length-1));
for(var i = 0; i < k ;i++){r=r+'0';}
return r;
}

built from scratch, tried + tested

poppyrich

3:42 pm on Oct 9, 2008 (gmt 0)

10+ Year Member



The toFixed() method (which returns a string, so be careful to use parseFloat) has been around in IE since ver 6 so I'm assuming it exists pretty much accross the board but I could be wrong.
If right, one line will do it - here it is wrapped in a function:

function roundtotwoplaces(a){
var y=parseFloat(((Math.round(a*100))/100).toFixed(2));
return y
}

alert(roundtotwoplaces(123.4567));

(Should return floating point number 123.46)

Just checked: works in FF2.
Opera 9.5 also.

Fotiman

4:21 pm on Oct 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Correction: toFixed() was included in IE 5.5.

poppyrich

6:24 pm on Oct 9, 2008 (gmt 0)

10+ Year Member



Thanks Fotiman. (Where'd you look that up?)

So I guess this solution is in pretty good shape cross-browser wise.

What's left, Safari?

Safari 3.1 for Windows works. I guess any Webkit browser will be OK, then.

(Not checking Chrome. They can shove Chrome where the sun don't shine. I've got enough browsers to test against. Don't need, don't want another one.)

[edited by: poppyrich at 6:36 pm (utc) on Oct. 9, 2008]

Dabrowski

6:33 pm on Oct 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If right, one line will do it

I counted 2? ;)

poppyrich

6:37 pm on Oct 9, 2008 (gmt 0)

10+ Year Member



Sorry, Dabrowski, you're right.
Amended:

function roundtotwoplaces(a){
return parseFloat(((Math.round(a*100))/100).toFixed(2));
}

atjhza

7:02 pm on Oct 9, 2008 (gmt 0)

10+ Year Member



Wow, Thanks everyone. This is great but I don't know where in the code I should enter the 2 lines of code from poppyrich. Sorry for being dense but if anyone could point it out for me, that would be great!

poppyrich

8:02 pm on Oct 9, 2008 (gmt 0)

10+ Year Member



LOL :)

Maybe somebody else is willing to catch this, but if you give me a day or so, I will.
busy elsewhere at moment but will find the time over the next 48 if you can wait.

httpwebwitch

8:18 pm on Oct 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



ha!

Your function hogs three whole lines.

I can do it in one line.

function roundtotwoplaces(a){return parseFloat(((Math.round(a*100))/100).toFixed(2));}

1up! 1up! 1up! kaching! kaching! kaching! I win. ;)

httpwebwitch

8:25 pm on Oct 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



whoops! this function does not pad the end with zeroes, tested using IE7.

atjhza

8:33 pm on Oct 9, 2008 (gmt 0)

10+ Year Member



thank you poppyrich.......this'll teach me to start something that I can't finish! Must start reading php!

:-)

httpwebwitch

8:34 pm on Oct 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



atjhza,

put the cround function right at the beginning after the <script> tag.

<script language="JavaScript"> 
<!--
function cround(floatnum,dec){
var r = (Math.round(floatnum * Math.pow(10,dec))/Math.pow(10,dec)).toString();
var p = r.lastIndexOf('.');
if(p==-1){r=r+'.'}
k = dec - (p==-1?0:(r.substring(p).length-1));
for(var i = 0; i < k ;i++){r=r+'0';}
return r;
}

function CalculateTotals(){
f=document.orderform;
...

then use it when you're assigning the value to your form elements. for example:

f.total1.value=cround(f.qty1.value*1.1,2);
f.total2.value=cround(f.qty2.value*1.00,2);
f.total3.value=cround(f.qty3.value*1.50,2);
f.total4.value=cround(f.qty4.value*0.5,2);

httpwebwitch

8:36 pm on Oct 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



welcome to WebmasterWorld, atjhza!

if you're keen to learn PHP, you must start lurking over at the PHP forum [webmasterworld.com].

httpwebwitch

8:50 pm on Oct 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



you can replace your entire script with this

<script language="JavaScript"> 
<!--
function cround(floatnum,dec){
var r = (Math.round(floatnum * Math.pow(10,dec))/Math.pow(10,dec)).toString();
if(r == 'NaN'){r='0'}
var p = r.lastIndexOf('.');
if(p==-1){r=r+'.'}
k = dec - (p==-1?0:(r.substring(p).length-1));
for(var i = 0; i < k ;i++){r=r+'0';}
return r;
}
function CalculateTotals(){
f=document.orderform;
var amt = [1.1,1.1,1,1.5,0.5,2,1,2.5,2,3,2.5,3.5,3,1,0.8,0.9];
var grandtotal = 0;
for(var i=1;i<16;i++){
f['total'+i].value = cround(parseFloat(f['qty'+i].value) * amt[i],2);
if(parseFloat(f['qty'+i].value) != 'NaN'){grandtotal += parseFloat(f['qty'+i].value) * amt[i];}
}
f['grandtotal'].value=cround(grandtotal,2);
}
//-->
</script>

Dabrowski

12:00 am on Oct 10, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



welcome to WebmasterWorld, atjhza

LOL! A bit late webwitch! :-D

var amt = [1.1,1.1,1,1.5,0.5,2,1,2.5,2,3,2.5,3.5,3,1,0.8,0.9];

That is uuuuuugly! Shame on you.

httpwebwitch

3:48 am on Oct 10, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



@Dabrowski -> ha! it's an array! deal with it! :O LOL

poppyrich

2:52 pm on Oct 11, 2008 (gmt 0)

10+ Year Member



@atjhza

Using your existing code, here's what you need to do to get the currency formatting (with zeroes) going:

1) Add this function inside the script tag:

function aC(a){
return (a==0)?'0':((Math.round(a*100))/100).toFixed(2);
}

(I chose the name aC for brevity and as a mnemonic for "As Currency")

2) For each line where you are multiplying the Qty by the price, send the result to the aC function for formatting like so:

f.total1.value=aC(parseFloat(f.qty1.value)*1.1);

f.total2.value=aC(parseFloat(f.qty2.value)*1);

f.total3.value=aC(parseFloat(f.qty3.value)*1.5);

and on and on....

Then, further down in the next section of CalculateTotals()...

3) Instead of

var grandtotal=parseFloat(f.total1.value)

Add the three letters "aC(" to it like this: var grandtotal=aC(parseFloat(f.total1.value)

Then, close the open paren all the way down at the bottom after everything's been added together by adding a ")":

+parseFloat(f.total15.value)
); // additional closing paren inserted here
} // closing curly brace of CalculateTotals function (already there)

If anything is not clear, sticky me.

As for the other code presented here in previous posts:

The way httpwebwitch is handling things is pretty much the way I would, given the way you've set up the page and named things, but the question is do you have the vaguest idea what's going on with that code?
What you've done is more verbose and less sophisticated than what someone more advanced in javascript would do, but so what? It's where you're at at the moment and you understand it well enough to make changes as you go along.
Or, you could give the other code a try and if you're comfortable with it, go with that. Haven't tested it but I'm assuming it, too, works OK.

Hope this helps.