PDA

View Full Version : Rounding off decimal places

chrispols
12-06-2006, 10:25 AM
Hi,
I have a script that does a great job of calculations of numbers. However, it calculates to 333.867756656 sometimes. How can I make the script round off the last 2 digits to make it 333.87 during the calculation?

Here is the following code:

<script type="text/javascript">
<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->
<!-- Created by: www.jdstiles.com -->
<!-- Begin
function startCalc(){
interval = setInterval("calc()",1);
}
function calc(){
two = document.form1.Documentation_Fee.value;
three = document.form1.VAT.value = ((one * 1) + (two * 1)) * ((14 / 100) * 1) ;
document.form1.Total_Due.value = (one * 1) + (two * 1) + (three * 1) ;
P1 = document.form1.Premium_Dealer.value = (one * 1) * ((12.5 / 100) * 1) ;
P2 = document.form1.Premium_CheckPlan.value = (one * 1) * ((24 / 100) * 1) ;
P3 = document.form1.Premium_Natsure.value = (one * 1) * ((7.5 / 100) * 1) ;
P4 = document.form1.Premium_QSales.value = (one * 1) * ((7.5 / 100) * 1) ;
P5 = document.form1.Premium_Reserves.value = (one * 1) * ((40.5 / 100) * 1) ;
P6 = document.form1.Premium_Assupol.value = (one * 1) * ((2 / 100) * 1) ;
document.form1.Premium_Agents.value = (one * 1) - ((P1 * 1) + (P2 * 1) + (P3 * 1) + (P4 * 1) + (P5 * 1) + (P6 * 1) * 1) ;
}
function stopCalc(){
clearInterval(interval);
}
// End -->
</script>

Many thanks,

Chris

brandonH
12-06-2006, 10:43 AM
document.form1.Premium_Agents.value = Math.round((one * 1) - ((P1 * 1) + (P2 * 1) + (P3 * 1) + (P4 * 1) + (P5 * 1) + (P6 * 1) * 1)*100)/100;

this takes " (one * 1) - ((P1 * 1) + (P2 * 1) + (P3 * 1) + (P4 * 1) + (P5 * 1) + (P6 * 1) * 1) " and multiplies it by 100.
we do this to move the decimal place over 2. then we round the oucome number off using Math.round(). after rounding the number off, we then divide it by 100, which moves the decimal place back over 2, giving you a perfect rounded 2 decimal place number.

nikkiH
12-06-2006, 02:50 PM
You could also use toFixed(),
i.e.
myIntVar.toFixed(2);

http://www.pageresource.com/jscript/j_a_03.htm

brandonH
12-06-2006, 03:00 PM
I have experienced to many problems with using toFixed() lol. but yes that is another way.

nikkiH
12-07-2006, 10:21 PM
Yeah, I didn't realize how crappy it was in IE. Never mind. :D

Roy Gardiner
12-08-2006, 10:08 AM
...multiplies it by 100.
we do this to move the decimal place over 2. then we round the oucome number off using Math.round(). after rounding the number off, we then divide it by 100, which moves the decimal place back over 2, giving you a perfect rounded 2 decimal place number. I don't wish to be critical of your method, but is there no way to do this properly? Sensibly, the round function would have a 2nd argument where you could say at which level rounding should take place.

Coming from Rexx, where you never have to worry about numbers, Javascript is an unpleasant shock.

Mr J
12-08-2006, 06:54 PM
I've had this snippet for a few years, not sure if it is still any good

<script type="text/javascript">
<!--
function roundOff(value, precision){
value = "" + value
precision = parseInt(precision)

var whole = "" + Math.round(value * Math.pow(10, precision))
var decPoint = whole.length - precision

if(decPoint != 0){
result = whole.substring(0, decPoint)
result += "."
result += whole.substring(decPoint, whole.length)
}
else{
result = whole
}

return result
}
//-->
</script>