Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Rounding off decimal places

1. ## Rounding off decimal places

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

• Code:
`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.

• You could also use toFixed(),
i.e.
myIntVar.toFixed(2);

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

• I have experienced to many problems with using toFixed() lol. but yes that is another way.

• Yeah, I didn't realize how crappy it was in IE. Never mind.

• Originally Posted by brandonH
...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.

• I've had this snippet for a few years, not sure if it is still any good

PHP Code:
``` <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>  ```

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•