summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--numpy/core/arrayprint.py9
-rw-r--r--numpy/core/tests/test_arrayprint.py10
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)})