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