Truncate Decimal Places

1. Tristan Guest

## Truncate Decimal Places

if I have a number like 52.33434343, is there anyway i can get it down to 52.33?? right now i&#039m just using the Math.round method...but I need some decimal points...thanks!!

2. Dan Evans Guest

## RE: Truncate Decimal Places

This will do it:<BR><BR>var x = 12.3456;<BR>x = Math.round(x * 100) / 100;<BR><BR>Or you could knock up a function for it:<BR><BR>alert(myRound(12.3456, 2)) //for 2 decimal places<BR>function myRound(x, d) {<BR> d = Math.pow(10, d);<BR> return Math.round(x * d) / d;<BR>}

3. Romulus Guest

## RE: Truncate Decimal Places

Dan that was a nice sloution.But this function only truncates the number what if i want to round off a number to two decimal places then the above function will not work.What solution could there be?

4. Dan Evans Guest

## RE: Truncate Decimal Places

Sorry, maths was never my strong point, but this seems to round the number for me, e.g., <BR><BR>12.3450..(2dp) gets rounded to 12.35<BR>12.3449..(2dp) gets rounded to 12.34

#### Posting Permissions

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