I have a float number such as x=23392342.1
I would like to convert it to a string with engineering notation (with metric prefix)
http://en.wikip
This is a simple method with no dependencies:
def to_units(x_):
units = {-12: "T",-9: "G",-6: "M",-3: "K",0: "",3: "m",6: "µ",9: "n",12: "p",15: "f"}
k = -12
while x_ * 10.0**k < 1:
k += 3
return f"{x_*10.0**k}{units[k]}"
Example:
for i in range(-15,15):
print(f"{to_units(1*10.0**i)} \t {1*10.0**i:1,.15f}")
1.0f 0.000000000000001
10.0f 0.000000000000010
100.0f 0.000000000000100
1.0p 0.000000000001000
10.0p 0.000000000010000
100.0p 0.000000000100000
1.0n 0.000000001000000
10.0n 0.000000010000000
100.0n 0.000000100000000
1.0µ 0.000001000000000
10.0µ 0.000010000000000
100.0µ 0.000100000000000
1.0m 0.001000000000000
10.0m 0.010000000000000
100.0m 0.100000000000000
1.0 1.000000000000000
10.0 10.000000000000000
100.0 100.000000000000000
1.0K 1,000.000000000000000
10.0K 10,000.000000000000000
100.0K 100,000.000000000000000
1.0M 1,000,000.000000000000000
10.0M 10,000,000.000000000000000
100.0M 100,000,000.000000000000000
1.0G 1,000,000,000.000000000000000
10.0G 10,000,000,000.000000000000000
100.0G 100,000,000,000.000000000000000
1.0T 1,000,000,000,000.000000000000000
10.0T 10,000,000,000,000.000000000000000
100.0T 100,000,000,000,000.000000000000000