8000 BUG: Fix array printing with precision=0. · numpy/numpy@65e1e0e · GitHub
[go: up one dir, main page]

Skip to content

Commit 65e1e0e

Browse files
committed
BUG: Fix array printing with precision=0.
Values rounding to zero were formatted with negative precision.
1 parent 9914e60 commit 65e1e0e

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

numpy/core/arrayprint.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -626,9 +626,12 @@ def __call__(self, x, strip_zeros=True):
626626

627627

628628
def _digits(x, precision, format):
629-
s = format % x
630-
z = s.rstrip('0')
631-
return precision - len(s) + len(z)
629+
if precision > 0:
630+
s = format % x
631+
z = s.rstrip('0')
632+
return precision - len(s) + len(z)
633+
else:
634+
return 0
632635

633636

634637
class IntegerFormat(object):

numpy/core/tests/test_arrayprint.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,16 @@ def test_basic(self):
129129
np.set_printoptions(precision=4)
130130
assert_equal(repr(x), "array([ 1.5 , 0. , 1.2346])")
131131

132+
def test_precision_zero(self):
133+
np.set_printoptions(precision=0)
134+
for values, string in (
135+
([0.], " 0."), ([.3], " 0."), ([-.3], "-0."), ([.7], " 1."),
136+
([1.5], " 2."), ([-1.5], "-2."), ([-15.34], "-15."),
137+
([100.], " 100."), ([.2, -1, 122.51], " 0., -1., 123."),
138+
([0], "0"), ([-12], "-12"), ([complex(.3, -.7)], " 0.-1.j")):
139+
x = np.array(values)
140+
assert_equal(repr(x), "array([%s])" % string)
141+
132142
def test_formatter(self):
133143
x = np.arange(3)
134144
np.set_printoptions(formatter={'all':lambda x: str(x-1)})

0 commit comments

Comments
 (0)
0