rounding decimal places

1. Member
Join Date
Dec 1969
Posts
94

## rounding decimal places

I read a few articles about formatting numbers but Im still having some problems getting this to work on my page. for example:<BR><BR>function total () <BR> {<BR>pr = document.form.price.value;<BR>qu = document.form.quantity.value;<BR>pq = pr/qu;<BR>document.form.principal.value = pq;<BR>&#060;/script&#062;<BR>&#060;input Name="principal" type="text" maxlength="25" size="25" readonly&#062;<BR><BR>How can get the value to be 49.667 instead of 49.99666666666667?<BR><BR>Thanks again!<BR>Blake

2. Senior Member
Join Date
Dec 1969
Posts
96,118

## Math.round(1000*value)

So you have 49.666666667<BR><BR>You multiply by 1000 and get 49666.666667<BR><BR>You round that to the nearest integer and get 49667<BR><BR>Then, to output it, you do:<BR><BR>temp = Math.round(1000*value); // round<BR>temp = "" + temp; // convert to a string<BR>display = temp.substring(0,temp.length-3) + "." + temp.substring(temp.length-3)<BR><BR>Don&#039;t divide it by 1000 again or you could be right back where you started!<BR><BR>See http://www.irt.org and the JS FAQ for questions like this one.<BR><BR>

#### Posting Permissions

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