diff options
author | wrwrwr <git@wr.waw.pl> | 2016-10-01 21:44:38 +0200 |
---|---|---|
committer | wrwrwr <git@wr.waw.pl> | 2016-10-07 14:26:40 +0200 |
commit | 65e1e0e4ba0909a45f410a24360e97568e2bb66a (patch) | |
tree | 6aa6548b7d96b10a1a235d8353239a33765f1237 | |
parent | 9914e603fa620aa7d3ae9576772eb50df4b799ef (diff) | |
download | numpy-65e1e0e4ba0909a45f410a24360e97568e2bb66a.tar.gz |
BUG: Fix array printing with precision=0.
Values rounding to zero were formatted with negative precision.
-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)}) |