decimal numbers

wxjmfauth at gmail.com wxjmfauth at gmail.com
Sun Feb 16 07:19:55 EST 2014


Without any warranty.

>>> def z(r):
...     # r: int > 0
...     t = log10(r)
...     if t >= 12.0:
...         prefix = ''
...         prefix2 = ''
...     elif t >= 9.0:
...         prefix = 'giga'
...         prefix2 = 'G'
...         r = r / 1.0e9
...     elif t >= 6.0:
...         prefix = 'mega'
...         prefix2 = 'M'
...         r = r / 1.0e6
...     elif t >= 3.0:
...         prefix = 'kilo-'  # Netherlands std?
...         prefix2 = 'k'
...         r = r / 1.0e3
...     else:
...         prefix = ''
...         prefix2 = ''
... 
...     # correct for this language (Dutch?) ?
...     # kept as illustrative example
...     if r >= 2:
...         suffix = 's'
...     else:
...         suffix = ''
...     
...     # '.13g' to "cover the ranges" while "keeping precision"
...     num = format(r, '.13g')
...     s = 'de weerstand is ' + num + ' ' + prefix + 'ohm' + suffix
...     s = s + ' , R = ' + num + ' ' + prefix2 + 'Ω'
...     return s
...     
>>> a = [1, 2, 9, 20, 300, 1000 - 1, 1000, 1000 + 1, 2000, \
...     1000000 - 1, 1000000, 1000000 + 1, \
...     1000000000 - 1, 1000000000, 1000000000 + 1, 11123456789]
>>> 
>>> for e in a:
...     print(e, '-', z(e))
...     
1 - de weerstand is 1 ohm , R = 1 Ω
2 - de weerstand is 2 ohms , R = 2 Ω
9 - de weerstand is 9 ohms , R = 9 Ω
20 - de weerstand is 20 ohms , R = 20 Ω
300 - de weerstand is 300 ohms , R = 300 Ω
999 - de weerstand is 999 ohms , R = 999 Ω
1000 - de weerstand is 1 kilo-ohm , R = 1 kΩ
1001 - de weerstand is 1.001 kilo-ohm , R = 1.001 kΩ
2000 - de weerstand is 2 kilo-ohms , R = 2 kΩ
999999 - de weerstand is 999.999 kilo-ohms , R = 999.999 kΩ
1000000 - de weerstand is 1 megaohm , R = 1 MΩ
1000001 - de weerstand is 1.000001 megaohm , R = 1.000001 MΩ
999999999 - de weerstand is 999.999999 megaohms , R = 999.999999 MΩ
1000000000 - de weerstand is 1 gigaohm , R = 1 GΩ
1000000001 - de weerstand is 1.000000001 gigaohm , R = 1.000000001 GΩ
11123456789 - de weerstand is 11.123456789 gigaohms , R = 11.123456789 GΩ

jmf




More information about the Python-list mailing list