Print with units Revision 356435356532 (Wed Jul 01 2009 at 11:29) - Diff Link to this snippet: https://friendpaste.com/4gxERkmprkvTL4hWk4KL6p Embed: manni perldoc borland colorful default murphy trac fruity autumn bw emacs pastie friendly Show line numbers Wrap lines 1234567891011121314151617181920212223# -*- coding: utf-8 -*-# http://fr.wikipedia.org/wiki/Pr%C3%A9fixe_du_syst%C3%A8me_internationalimport sys, localeencoding = sys.stdout.encoding or locale.getpreferredencoding()numbers = [1.236, 1742, 0.5698, 4569874, 356, 0.000698]def printWithUnit(number): units = [(u'G', 1e9), (u'M', 1e6), (u'K', 1e3), (u' ', 1.0), (u'm', 1e-3), (u'µ', 1e-6), (u'n', 1e-9)] for unit, coeff in units: if number > coeff: return u"%5.1f %s" % (number / coeff, unit) return u"%.1g" % numberfor number in numbers: print(printWithUnit(number).encode(encoding))