[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**: "bestworldweb" <bestworldweb@...>
**Date**: Sat, 15 Mar 2003 09:18:40 -0000

thanks for the help
--- In lua-l@yahoogroups.com, John Mckenna <jmckenna@r...> wrote:
> From: bestworldweb [mailto:bestworldweb@y...]
>
> >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