8000 fix an issue with {:g} and str format · matplotlib/matplotlib@b75f20d · GitHub
[go: up one dir, main page]

Skip to content

Commit b75f20d

Browse files
committed
fix an issue with {:g} and str format
1 parent aba7a7f commit b75f20d

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

lib/matplotlib/ticker.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,16 +1285,15 @@ def format_eng(self, num):
12851285

12861286
mant = sign * dnum / (10 ** pow10)
12871287

1288-
if self.places is None:
1289-
fmt = "g"
1290-
elif self.places == 0:
1291-
fmt = "d"
1292-
else:
1293-
fmt = ".{}f".format(self.places)
1294-
1288+
# NB: one has to cast *mant* to a float because it is actually
1289+
# an instance of decimal.Decimal. Combined for `str.format`, this
1290+
# may produce strings with more than 6 digits in the case of the
1291+
# "%g" format, which breaks the former behavior that one got with
1292+
# C-style formatting. Another option would be to rely on the
1293+
# `decimal.localcontext()` context manager.
12951294
formatted = "{mant:{fmt}}{sep}{prefix}".format(
1296-
mant=mant, fmt=fmt, sep=self.sep, prefix=prefix)
1297-
1295+
mant=float(mant), sep=self.sep, prefix=prefix,
1296+
fmt="g" if self.places is None else ".{:d}f".format(self.places))
12981297

12991298
return formatted
13001299

0 commit comments

Comments
 (0)
0