@@ -1036,6 +1036,9 @@ class EngFormatter(Formatter):
1036
1036
`places` is the precision with which to display the number,
1037
1037
specified in digits after the decimal point (there will be between
1038
1038
one and three digits before the decimal point).
1039
+
1040
+ `space_sep` controls if there is a space between the number and the
1041
+ prefix/unit. For example, '3.14 mV' if True and '3.14mV' if False.
1039
1042
"""
1040
1043
# The SI engineering prefixes
1041
1044
ENG_PREFIXES = {
@@ -1058,9 +1061,27 @@ class EngFormatter(Formatter):
1058
1061
24 : "Y"
1059
1062
}
1060
1063
1061
- def __init__ (self , unit = "" , places = None ):
1064
+ def __init__ (self , unit = "" , places = None , space_sep = True ):
1065
+ """ Parameters
1066
+ ----------
1067
+ unit: str (default: u"")
1068
+ Unit symbol to use.
1069
+
1070
+ places: int (default: None)
1071
+ Precision, i.e. number of digits after the decimal point.
1072
+ If it is None, falls back to the floating point format '%g'.
1073
+
1074
+ space_sep: boolean (default: True)
1075
+ If True, a (single) space is used between the value and the
1076
+ prefix/unit, else the prefix/unit is directly appended to the
1077
+ value.
1078
+ """
1062
1079
self .unit = unit
1063
1080
self .places = places
1081
+ if space_sep is True :
1082
+ self .sep = u" " # 1 space
1083
+ else :
1084
+ self .sep = u"" # no space
1064
1085
1065
1086
def __call__ (self , x , pos = None ):
1066
1087
s = "%s%s" % (self .format_eng (x ), self .unit )
@@ -1104,18 +1125,20 @@ def format_eng(self, num):
1104
1125
1105
1126
mant = sign * dnum / (10 ** pow10 )
1106
1127
1128
+ # TODO: shouldn't we raise a warning if self.places < 0?
1107
1129
if self .places is None :
1108
- format_str = "%g %s"
1130
+ format_str = "%g{sep:s} %s" . format ( sep = self . sep )
1109
1131
elif self .places == 0 :
1110
- format_str = "%i %s"
1132
+ format_str = "%d{sep:s} %s" . format ( sep = self . sep )
1111
1133
elif self .places > 0 :
1112
- format_str = ("%%.%if %%s" % self .places )
1134
+ format_str = "%.{p:d}f{sep:s}%s" .format (p = self .places ,
1135
+ sep = self .sep )
1113
1136
1114
1137
formatted = format_str % (mant , prefix )
1115
1138
1116
1139
formatted = formatted .strip ()
1117
1140
if (self .unit != "" ) and (prefix == self .ENG_PREFIXES [0 ]):
1118
- formatted = formatted + " "
1141
+ formatted = formatted + self . sep
1119
1142
1120
1143
return formatted
1121
1144
0 commit comments