osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

decimal numbers


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