-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEngineeringNotation.py
53 lines (41 loc) · 1.19 KB
/
EngineeringNotation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"""
EngineeringNotation.py
Provides function to convert to engineering notation (similar to schetific notation).
Uses the 'decimal' library.
"""
def to_string(num_arg: float, precision_arg: int = 3) -> str:
import decimal
decimal.getcontext().prec = precision_arg
return decimal.Decimal(num_arg).normalize().to_eng_string()
# Test function for to_eng_string()
if __name__ == '__main__':
import math
print('Start.')
# Test cases taken from http://code.activestate.com/recipes/579046-engineering-notation/
# some string-based test cases added.
test = [
'-78951',
'-500',
'1e-3',
'0.005',
'0.05',
'0.12',
'10',
'23.3456789',
'50',
'150',
'250',
'800',
'1250',
'127e11',
'51234562',
str(math.pi),
str(1.0/3.0),
'inf',
'nan'
]
print("{0:>20}: {1:>20} {2:>10} {3:>14}".format('string', 'float', 'default', 'prec=8'))
for x in test:
f = float(x)
print("{0:>20}: {1:>20} {2:>10} {3:>14}".format(x, f, to_string(f), to_string(f, 8)))
print('Done.')