-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformatting.py
More file actions
60 lines (43 loc) · 1.75 KB
/
formatting.py
File metadata and controls
60 lines (43 loc) · 1.75 KB
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
54
55
56
57
58
59
60
"""module docstring should be here"""
import numpy as np
from typing import Final, Sequence
# Code based on
# https://stackoverflow.com/questions/21008858/formatting-floats-in-a-numpy-array
# and on
# https://stackoverflow.com/questions/2440692/formatting-floats-in-python-without-superfluous-zeros
#
def format_float(x: float) -> str:
return '{0:.3f}'.format(x).rstrip('0').rstrip('.')
def format_float_hq(x: float) -> str:
return '{0:.21f}'.format(x).rstrip('0').rstrip('.')
def format_floats(array: Sequence) -> str:
result = ''
length: Final[int] = len(array)
for i in range(0, length):
result += format_float(array[i])
if i != length - 1:
result += ' '
return result
def format_floats_hq(array: Sequence) -> str:
result = ''
length: Final[int] = len(array)
for i in range(0, length):
result += format_float_hq(array[i])
if i != length - 1:
result += ' '
return result
def format_np_floating(x: np.floating) -> str:
return format_float(float(x))
def main():
for x in 12.3456789000000, 12.0003456789000, 12345.6789, -0.000001:
print('x being ' + str(x) + f' (type(x) = {type(x)}):')
print(' » {} (using `.format(format_float(x)`)'.format(format_float(x)))
print(f' » {x:.3f} (using just 3 decimals with .3f)')
print(f' » {x:.3g} (using just 3 decimals with .3g)')
y: Final[np.floating] = np.float64(12.3456789000000)
print('y being ' + str(y) + f' (type(y) = {type(y)}):')
print(' » {} (using `.format(format_np_floating(y)`)'.format(format_np_floating(y)))
print(f' » {y:.3f} (using just 3 decimals with .3f)')
print(f' » {y:.3g} (using just 3 decimals with .3g)')
if __name__ == '__main__':
main()