aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2017-09-22 17:10:03 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2017-09-22 17:10:03 +0200
commitf63fd3e53172edc0eaff3abe0f3dfbc40edf2341 (patch)
treea491f8d1b9b79a5a63723d872cc86e9af7befb48
parenta7a5c583d295c70f63898a005bd00b7d84412478 (diff)
downloadee-python-f63fd3e53172edc0eaff3abe0f3dfbc40edf2341.tar.gz
ee-python-f63fd3e53172edc0eaff3abe0f3dfbc40edf2341.tar.bz2
ee-python-f63fd3e53172edc0eaff3abe0f3dfbc40edf2341.tar.xz
ee-python-f63fd3e53172edc0eaff3abe0f3dfbc40edf2341.zip
o Handling smaller numbers that we have units for.
-rw-r--r--src/ee/formatting/__init__.py12
-rw-r--r--test/test_formatting.py4
2 files changed, 12 insertions, 4 deletions
diff --git a/src/ee/formatting/__init__.py b/src/ee/formatting/__init__.py
index 3fa82d2..5e612fe 100644
--- a/src/ee/formatting/__init__.py
+++ b/src/ee/formatting/__init__.py
@@ -91,9 +91,15 @@ def eng_str(value, unit=None):
suffixes = small
idx = int(-div3)
- suffix = suffixes[idx]
- scale = 10 ** (div3 * 3)
- scaled = value / scale
+ if idx < len(suffixes):
+ suffix = suffixes[idx]
+ scale = 10 ** (div3 * 3)
+ scaled = value / scale
+ else:
+ idx = len(suffixes) - 1
+ suffix = suffixes[idx]
+ scale = 10 ** (3 * idx)
+ scaled = scale * value
s = "{:1.3g}{}".format(scaled, suffix)
diff --git a/test/test_formatting.py b/test/test_formatting.py
index 642b3de..bcb9300 100644
--- a/test/test_formatting.py
+++ b/test/test_formatting.py
@@ -81,6 +81,8 @@ def xx_test_eng_str3(input, expected):
("1 n", 0.000000001, "1 n", None),
("10 n", 0.00000001, "10 n", None),
("1p", 1e-12, "1 p", None),
+ ("0.8p", 8e-13, "0.8 p", None),
+ ("0.008p", 8e-15, "0.008 p", None),
])
def test_EeVal(s, num_value, str_value, unit):
num_value = float(num_value)
@@ -95,7 +97,7 @@ def test_EeVal(s, num_value, str_value, unit):
("10nF", "10 nF", "F"),
("10n F", "10 nF", "F"),
("10 n F", "10 nF", "F"),
-# ("4.7 n F", "4.7 uF", "F"),
+ ("4.7 n F", "4.7 nF", "F"),
])
def test_EeVal_with_units(s, str_value, unit):
v = EeVal(s)