Thanks to visit codestin.com Credit goes to github.com
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 0bde03a commit c98ba91Copy full SHA for c98ba91
2 files changed
lib/matplotlib/tests/test_ticker.py
@@ -564,11 +564,13 @@ class TestEngFormatter(object):
564
(0.1, ('100 m', '100 m', '100.00 m')),
565
(1, ('1', '1', '1.00')),
566
(1.23456789, ('1.23457', '1', '1.23')),
567
- (999.9, ('999.9', '1 k', '999.90')),
+ (999.9, ('999.9', '1 k', '999.90')), # places=0: corner-case rounding
568
+ (999.9999, ('1 k', '1 k', '1.00 k')), # corner-case roudning for all
569
(1000, ('1 k', '1 k', '1.00 k')),
570
(1001, ('1.001 k', '1 k', '1.00 k')),
571
(100001, ('100.001 k', '100 k', '100.00 k')),
- (987654.321, ('987.654 k', '988 k', '987.65 k'))
572
+ (987654.321, ('987.654 k', '988 k', '987.65 k')),
573
+ (1.23e27, ('1230 Y', '1230 Y', '1230.00 Y')) # OoR value (> 1000 Y)
574
]
575
576
@pytest.mark.parametrize('input, expected', raw_format_data)
lib/matplotlib/ticker.py
@@ -1265,6 +1265,7 @@ def format_eng(self, num):
1265
"""
1266
dnum = float(num)
1267
sign = 1
1268
+ fmt = "g" if self.places is None else ".{:d}f".format(self.places)
1269
1270
if dnum < 0:
1271
sign = -1
@@ -1283,17 +1284,18 @@ def format_eng(self, num):
1283
1284
1285
mant = sign * dnum / (10.0 ** pow10)
1286
# Taking care of the cases like 999.9..., which
- # may be rounded to 1000 instead of 1 k.
1287
- if (self.places is not None and
1288
- round(mant, self.places) >= 1000):
+ # may be rounded to 1000 instead of 1 k. Beware
+ # of the corner case of values that are beyond
1289
+ # the range of SI prefixes (i.e. > 'Y').
1290
+ _fmant = float("{mant:{fmt}}".format(mant=mant, fmt=fmt))
1291
+ if _fmant >= 1000 and pow10 != max(self.ENG_PREFIXES):
1292
mant /= 1000
1293
pow10 += 3
1294
1295
prefix = self.ENG_PREFIXES[int(pow10)]
1296
1297
formatted = "{mant:{fmt}}{sep}{prefix}".format(
- mant=mant, sep=self.sep, prefix=prefix,
- fmt="g" if self.places is None else ".{:d}f".format(self.places))
1298
+ mant=mant, sep=self.sep, prefix=prefix, fmt=fmt)
1299
1300
return formatted
1301
0 commit comments