diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2016-10-07 10:04:49 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-10-07 10:04:49 -0500 |
commit | fda7484cb572c8e6b50173784cb1498aca03a4e5 (patch) | |
tree | c78f1166c7cb2797e9b854505f6c2adcc6688f2f | |
parent | fa214c1f75e6f1bfbb64eafa206049e3efe19763 (diff) | |
parent | 65e1e0e4ba0909a45f410a24360e97568e2bb66a (diff) | |
download | numpy-fda7484cb572c8e6b50173784cb1498aca03a4e5.tar.gz |
Merge pull request #8107 from wrwrwr/fix-precision-zero
BUG: Fix array printing with precision=0.
-rw-r--r-- | numpy/core/arrayprint.py | 9 | ||||
-rw-r--r-- | numpy/core/tests/test_arrayprint.py | 10 |
2 files changed, 16 insertions, 3 deletions
diff --git a/numpy/core/arrayprint.py b/numpy/core/arrayprint.py index b05082e9d..cd618d72a 100644 --- a/numpy/core/arrayprint.py +++ b/numpy/core/arrayprint.py @@ -626,9 +626,12 @@ class FloatFormat(object): def _digits(x, precision, format): - s = format % x - z = s.rstrip('0') - return precision - len(s) + len(z) + if precision > 0: + s = format % x + z = s.rstrip('0') + return precision - len(s) + len(z) + else: + return 0 class IntegerFormat(object): diff --git a/numpy/core/tests/test_arrayprint.py b/numpy/core/tests/test_arrayprint.py index 5759a0984..991ead973 100644 --- a/numpy/core/tests/test_arrayprint.py +++ b/numpy/core/tests/test_arrayprint.py @@ -129,6 +129,16 @@ class TestPrintOptions: np.set_printoptions(precision=4) assert_equal(repr(x), "array([ 1.5 , 0. , 1.2346])") + def test_precision_zero(self): + np.set_printoptions(precision=0) + for values, string in ( + ([0.], " 0."), ([.3], " 0."), ([-.3], "-0."), ([.7], " 1."), + ([1.5], " 2."), ([-1.5], "-2."), ([-15.34], "-15."), + ([100.], " 100."), ([.2, -1, 122.51], " 0., -1., 123."), + ([0], "0"), ([-12], "-12"), ([complex(.3, -.7)], " 0.-1.j")): + x = np.array(values) + assert_equal(repr(x), "array([%s])" % string) + def test_formatter(self): x = np.arange(3) np.set_printoptions(formatter={'all':lambda x: str(x-1)}) |