File tree Expand file tree Collapse file tree 1 file changed +8
-9
lines changed Expand file tree Collapse file tree 1 file changed +8
-9
lines changed Original file line number Diff line number Diff line change @@ -1285,16 +1285,15 @@ def format_eng(self, num):
1285
C307
td>1285
1286
1286
mant = sign * dnum / (10 ** pow10 )
1287
1287
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.
1295
1294
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 ))
1298
1297
1299
1298
return formatted
1300
1299
You can’t perform that action at this time.
0 commit comments