arrays, even, roundup, odd round down ?

Ben Finney bignose+hates-spam at benfinney.id.au
Tue May 16 18:57:10 EDT 2006


Lance Hoffmeyer <lance at augustmail.com> writes:

> So, I have using the following to grab numbers from MS Word.  I
> discovered that that there is a "special" rule being used for
> rounding.
> 
> If a ??.5 is even the number is to rounded down (20.5 = 20)
> if a ??.5 is odd the number is to rounded up (21.5 = 22)

This sounds specific enough that I'd want it in a separate function.

    def specially_rounded_integer(num):
        """ Round a number using our special rules """
        rounded_num = num * phase_of_moon()    # or whatever else you need to do
        return rounded_num

> Brands = ["B1","B2"]
> A1 = []
> A1 = [ re.search(r"(?m)(?s)\r%s.*?SECOND.*?(?:(\d{1,3}\.\d)\s+){2}" % i, target_table).group(1)  for i in Brands ]
> A1 = [int(float(str(x))+0.5) for x in A1 ]
> print A1

    original_nums = however_you_get_them()
    rounded_nums = [specially_rounded_integer(n) for n in original_nums]

If it's not quickly obvious on a single line, express it more
fully. Future readers, including yourself, will thank you.

-- 
 \        "Nothing in life is so exhilarating as to be shot at without |
  `\                                    result."  -- Winston Churchill |
_o__)                                                                  |
Ben Finney




More information about the Python-list mailing list