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: Round number 2 decimal points

1. ## Round number 2 decimal points

Hello:

I have this js function:

Code:
<script type="text/javascript">
document.PaymentForm.newbalance.value = (document.PaymentForm.balance.value -0) - (document.PaymentForm.paymentamount.value -0);
}
</script>
I want to round the value of
Code:
document.PaymentForm.newbalance.value
to two decimal points.

May I request some assistance!

• toFixed(2)

PHP Code:
var textValue document.PaymentForm.newbalance.value;
var
floatValue parseFloat(textValue);

var
formatedFloat floatValue.toFixed(2

• ## Users who have thanked barkermn01 for this post:

mbarandao (12-06-2010)

• Much oblige!

• toFixed() returns a string value, so if the number is to be subject to further calculation either it must be converted back to a number with Number or parseFloat(), or the following method used to round to 2DP.

Code:
<script type = "text/javascript">

function roundNumber(num, dec) {
var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
return result;
}

</script>

• Thanks Philip M for further explaining the toFixed().

If my function that performs the calculation is this:
Code:
<script type="text/javascript">
document.PaymentForm.newbalance.value = (document.PaymentForm.balance.value -0) - (document.PaymentForm.paymentamount.value -0);
}
</script>
How do I incorporate your suggested function
Code:
<script type = "text/javascript">

function roundNumber(num, dec) {
var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
return result;
}

</script>
into one to thus do the calculation and round the number?

• Code:
<script type="text/javascript">
var bal = document.PaymentForm.balance.value - document.PaymentForm.paymentamount.value;
bal = Math.round(bal*Math.pow(10,2))/Math.pow(10,2);
document.PaymentForm.newbalance.value = bal;
}
</script>

• ## Users who have thanked Philip M for this post:

mbarandao (12-07-2010)

• Man, such a headache you have saved me from. Thank you sir!

I'm quite new to js--and still learning

Again, Thanks!
Mossa

• Rounding 12345.00 or 12345 to 2 decimal places will result in 12345 which may not be what you want.

You should use toFixed() for display purposes but the code given for calculation purposes. So perhaps you need both forms:-

Code:
<script type="text/javascript">
var bal = document.PaymentForm.balance.value - document.PaymentForm.paymentamount.value;
var calcBal = Math.round(bal*Math.pow(10,2))/Math.pow(10,2);  // the value for calculations
var displayBal = bal.toFixed(2);  // the value to be displayed
document.PaymentForm.newbalance.value = displayBal;
}
</script>

• Great thought! I did in fact reproduce a similar condition during testing. This secondary suggestion perhaps addresses this.

Again, thank you kindly!
Mossa

•

#### Posting Permissions

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