On 11/8/11 Tue  Nov 8, 2011  11:02 AM, "Noah" <noah-l...@enabled.com>
scribbled:

> Hi there,
> 
> I am trying to round to 10 or 100 for particular values
> 
> so the following numbers
> 530
> 570
> 2330
> 2590
> 3630
> 3660
> 
> would become
> 500
> 600
> 2300
> 2600
> 3600
> 3700
> 
> any clues how to do this well?

Decide under what conditions you will be rounding to 10 and when you will be
rounding to 100 (all your examples use 100).

Decide what you want if a value is halfway between the two closest candidate
values.

Read what other people do (google for 'perl rounding').

Read what the int function does ('perldoc -f int').

Read about the Math::Round module ('perldoc Math:Round').

Don't worry about efficiency; worry about accuracy.

Write lots of test cases.

The general algorithm for rounding to a multiple of R (R = 10 or 100) would
be:

1. Divide your value by R
2. Add 0.5
3. Truncate using int()
4. Multiply by R

but you may want to alter the above for edge cases such as the
halfway-between case.



-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to