Kim Branson wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi all,

heres a quick one for you,
I have a series of data that I am using dictionaries to build histograms for. I'd like to round the data to the nearest 10, i.e if the value is 15.34 should we round down to 10? and conversely rounding say 19.30 to 20. I'm thinking 15.5 and above would round up. Can anyone point me a at a quick and painless way of achieving this?



def round10s(f): rem = f % 10 tens = int(f / 10)

    if rem >= 5.5:
        return (tens + 1.0) * 10.0
    return tens * 10.0

if __name__ == '__main__':
    tests = [15.34, 15.54, 19.65, 2.34, 8.2, 0.0, 20.0]
    for i in tests:
        print "%.2f: %.2f" % (i, round10s(i))

yields
15.34: 10.00
15.54: 20.00
19.65: 20.00
 2.34:  0.00
 8.20: 10.00
 0.00:  0.00
20.00: 20.00
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to