@@ -1192,6 +1192,9 @@ class EngFormatter(Formatter):
1192
1192
`places` is the precision with which to display the number,
1193
1193
specified in digits after the decimal point (there will be between
1194
1194
one and three digits before the decimal point).
1195
+
1196
+ `space_sep` controls if there is a space between the number and the
1197
+ prefix/unit. For example, '3.14 mV' if True and '3.14mV' if False.
1195
1198
"""
1196
1199
# The SI engineering prefixes
1197
1200
ENG_PREFIXES = {
@@ -1214,9 +1217,27 @@ class EngFormatter(Formatter):
1214
1217
24 : "Y"
1215
1218
}
1216
1219
1217
- def __init__ (self , unit = "" , places = None ):
1220
+ def __init__ (self , unit = "" , places = None , space_sep = True ):
1221
+ """ Parameters
1222
+ ----------
1223
+ unit: str (default: u"")
1224
+ Unit symbol to use.
1225
+
1226
+ places: int (default: None)
1227
+ Precision, i.e. number of digits after the decimal point.
1228
+ If it is None, falls back to the floating point format '%g'.
1229
+
1230
+ space_sep: boolean (default: True)
1231
+ If True, a (single) space is used between the value and the
1232
+ prefix/unit, else the prefix/unit is directly appended to the
1233
+ value.
1234
+ """
1218
1235
self .unit = unit
1219
1236
self .places = places
1237
+ if space_sep is True :
1238
+ self .sep = u" " # 1 space
1239
+ else :
1240
+ self .sep = u"" # no space
1220
1241
1221
1242
def __call__ (self , x , pos = None ):
1222
1243
s = "%s%s" % (self .format_eng (x ), self .unit )
@@ -1260,18 +1281,20 @@ def format_eng(self, num):
1260
1281
1261
1282
mant = sign * dnum / (10 ** pow10 )
1262
1283
1284
+ # TODO: shouldn't we raise a warning if self.places < 0?
1263
1285
if self .places is None :
1264
- format_str = "%g %s"
1286
+ format_str = "%g{sep:s} %s" . format ( sep = self . sep )
1265
1287
elif self .places == 0 :
1266
- format_str = "%i %s"
1288
+ format_str = "%d{sep:s} %s" . format ( sep = self . sep )
1267
1289
elif self .places > 0 :
1268
- format_str = ("%%.%if %%s" % self .places )
1290
+ format_str = "%.{p:d}f{sep:s}%s" .format (p = self .places ,
1291
+ sep = self .sep )
1269
1292
1270
1293
formatted = format_str % (mant , prefix )
1271
1294
1272
1295
formatted = formatted .strip ()
1273
1296
if (self .unit != "" ) and (prefix == self .ENG_PREFIXES [0 ]):
1274
- formatted = formatted + " "
1297
+ formatted = formatted + self . sep
1275
1298
1276
1299
return formatted
1277
1300
0 commit comments