Click to See Complete Forum and Search --> : formatting numbers to 2 decimal places


Toka
02-20-2004, 02:09 AM
Hi

I have some code where I take a number in from an input box, i then call a function which does some calculations and outputs the result to an output box (test field).

Does anyone know how I can get the output formatted to only 2 decimal places.

Thanks in advance

Toka

fredmv
02-20-2004, 02:16 AM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>untitled</title>
<meta http-equiv="content-type" content="application/xhtml+xml; charset=iso-8859-1" />
<script type="text/javascript">
//<![CDATA[
function output(s)
{
alert(Number(s).toFixed(2));
return false;
}
//]]>
</script>
</head>
<body>
<form action="#" onsubmit="return output(elements[0].value);">
<div>
<input type="text" />
<input type="submit" value="Submit" />
</div>
</form>
</body>
</html>

Toka
02-20-2004, 07:27 AM
I have a function as follows how can I adapt the above code to work for this output.

function inputtest(form) {
var val = "-" + tonum(form.inField.value);
var result;

if (val <=-6 && val >=-20) {
result = solve_quad(val);
}
else {
if (val<=-20 && val >=-50) {
result = solve_quad2(val);
}
else {
if (val<-50) {
result = solve_quad3(val);
}
}
}

form.outField.value = result;
}


The code I have for the output is as follows


<input type="button" name="btn12" value="Solve" onClick="inputtest(this.form)" />

Toka
02-20-2004, 10:01 AM
Hi

For anyone with the same problem I found an easy answer

this works a treat

result=(Math.round(result*100)/100);