[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: RE: How do I round number to x decimal places eg to round 12.5000 0009 to 12.5
- From: John Mckenna <jmckenna@...>
- Date: Thu, 13 Mar 2003 09:11:45 -0000
From: bestworldweb [mailto:bestworldweb@yahoo.co.nz]
>How do I round number to x decimal places eg to round 12.50000009 to
>12.5?
Easy! Multiply the number by a power of 10 to get all of the digits you
want to keep above the decimal point. Truncate it to an integer. Then
divide to get everything back where it started:
x = 12.50000009
digits = 1 -- you want 1 digit after the decimal point
shift = 10 ^ digits
result = floor( x*shift + 0.5 ) / shift
I'm adding 0.5 so it rounds (12.5400009 would become 12.5, 12.55000009 would
become 12.6)
-Virus scanned and cleared ok