...

View Full Version : Rounding query



Eski
07-31-2002, 10:20 AM
I am very new to JavaScript and have been looking how to round a number to two decimal places.

From looking at the tutorial at http://www.javascriptkit.com/javatutors/round.shtml
I added "var result=Math.round(tempnum*100)/100000}"
to the final line of my page, but it does not work!!


<SCRIPT LANGUAGE="JavaScript">
<!--
function calc(f)
{
vx = parseInt(f.x.value);
da = new Date(f.a.value);
db = new Date(f.b.value);
dd = (db.valueOf()-da.valueOf())/1000/60/60/24;
vc = parseInt(f.c.value);
vd = parseInt(f.d.value);

f.answer.value=(vx/365)*dd-vc+vd
function roundoff(amount) {
return Math.round(amount)
function decimal(num){
var tempnum=num
for (i=0;i<tempnum.length;i++){
if (tempnum.charAt(i)=="."){
break
}
if (tempnum.charAt(tempnum.length-2)=="."){
num=tempnum+"0"
}
else{
num=tempnum
}
var result=Math.round(num*100)/100};

}
}
}
}
//-->
</SCRIPT>


Many thanks,
Eski

requestcode
07-31-2002, 01:12 PM
Here is an example that works for me:
<html>
<head>
<title>Rounding Numbers</title>
<SCRIPT LANGUAGE="JavaScript">
function calc(formid)
{
re=/\./gi
// convert the two values to numbers
numa=parseFloat(formid.txta.value)
numb=parseFloat(formid.txtb.value)
numc=numa+numb
formid.txtc.value=numc
// round to two decimal positions
var number=Math.round(numc * 100)/100
// convert back to string
var numd=number.toString()
nume=numd.split(".")
numelen=nume.length
if(numelen<2) // if whole number add decimal point and two zeros
{
numd+=".00"
}
else
{
if(nume[1].length<2) // if only one digit after decmal point add zero
{numd+="0"}
}
formid.txtd.value=numd
}
</SCRIPT>
</head>
<body>
<FORM NAME="myform">
First number <INPUT TYPE="text" NAME="txta" SIZE="10">
<br>
Second Number <INPUT TYPE="text" NAME="txtb" SIZE="10">
<br>
Before rounding<INPUT TYPE="text" NAME="txtc" SIZE="15">
<br>
After rounding <INPUT TYPE="text" NAME="txtd" SIZE="15">
<br>
<INPUT TYPE="button" NAME="but1" VALUE="Add" onClick="calc(this.form)">
</FORM>
</body>
</html>

Eski
07-31-2002, 02:22 PM
Hi, and thanks for you help, but....

I see how your line:
var number=Math.round(numc * 100)/100

works for you, but when I use this on my page (substiuting numc), it still doesn't work!!!

I have used Math.round function, but followed by (amount) . is this what is causing the problem?

Sorry to sound such a dunce!!!

Eski

RadarBob
07-31-2002, 06:39 PM
Don't know what all that code is about.... here is the basics.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Untitled</title>
</head>
<body>
<script language="JavaScript" type="text/javascript">
<!--
roundedNum = 25.12543;
alert ("Number starts out as " + roundedNum);

roundedNum = Math.round(roundedNum*100)/100;
alert ("Rounded to 2 places. = " + roundedNum);
//-->
</script>

</body>
</html>


The technique: "grab" two decimal digits by multiplying by 100. Round the result. Undo the "grab" by dividing by the same value - 100 in this case. Ta da!:thumbsup:


Also, you've got functions defined inside of other functions (look where the terminating brackets are!). That's a no-no. Fix that and maybe your problem is fixed.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum