3 """Format values with nice prefixes.
8 * added negative number support
9 * added decimal-formatted output
14 from numpy import isnan
16 def pretty_format(fValue, sUnit='', iDecimals=-1, iMultiplier=1, bLeadingSpaces=False):
22 iMultiplier=get_multiplier(fValue)
25 sUnitString = ' ' + get_prefix(iMultiplier) + sUnit
27 formatString = '% ' + repr(iLeadingSpaces + iDecimals) + '.' + repr(iDecimals) + 'f'
28 return formatString % (fValue / iMultiplier) + sUnitString
30 return str(fValue / iMultiplier) + sUnitString
33 return str(fValue / iMultiplier) + ' ' + get_prefix(fValue / iMultiplier) + sUnit
35 def get_multiplier(fValue):
36 return pow(10, get_power(fValue))
38 def get_power(fValue):
39 if fValue != 0 and not isnan(fValue):
40 #get the log10 from fValue (make sure the value is not negative)
41 dHelp = math.floor(math.log10(math.fabs(fValue)))
42 #reduce the log10 to a multiple of 3 and return it
43 return dHelp-(dHelp % 3)
47 def get_prefix(fValue):
48 #set up a dictionary to find the prefix
60 -6: lambda: u'\u00B5',
68 if fValue != 0 and not isnan(fValue):
69 #get the log10 from fValue
70 dHelp = math.floor(math.log10(math.fabs(fValue)))
73 #reduce the log10 to a multiple of 3 and create the return string
74 return prefix.get(dHelp - (dHelp % 3))()
77 dTestValue=-2.4115665714484597e-008
78 print 'Value: '+str(dTestValue)+')'
79 print 'pretty_format example (value, unit)'
80 print pretty_format(dTestValue, 'N')
81 print'-----------------------'
82 print 'pretty_format example (value, unit, decimals)'
83 print pretty_format(dTestValue, 'N', 3)
84 print'-----------------------'
85 print 'pretty_format example (value, unit, decimals, multiplier)'
86 print pretty_format(dTestValue, 'N', 5, 0.000001)
87 print'-----------------------'
88 print 'pretty_format example (value, unit, decimals, multiplier, leading spaces)'
89 print pretty_format(0.0166276297705, 'N', 3, 0.001, True)
90 print pretty_format(0.00750520813323, 'N', 3, 0.001, True)
91 print pretty_format(0.0136453282825, 'N', 3, 0.001, True)
94 #example use autoFormatValue
95 dTestValue=0.00000000567
96 print 'autoFormatValue example ('+str(dTestValue)+')'
97 print autoFormatValue(dTestValue, 'N')
101 #example use of decimalFormatValue(fValue, iDecimals, sUnit):
102 dTestValue=-2.4115665714484597e-008
104 print 'decimalFormatValue example ('+str(dTestValue)+')'
105 print decimalFormatValue(dTestValue, iDecimals, 'N')
107 #change iDecimals to see the effect
110 #example use formatValue
111 dTestValue=0.000000000567
112 print 'formatValue example ('+str(dTestValue)+')'
113 #find the (common) multiplier
114 iMultiplier=get_multiplier(dTestValue)
115 #use the multiplier and a unit to format the value
116 print formatValue(dTestValue, iMultiplier, 'N')
121 #choose any value on the axis and find the multiplier and prefix for it
122 #use those to format the rest of the scale
123 #as values can span several orders of magnitude, you have to decide what units to use
126 scaleValues=0.000000000985, 0.000000001000, 0.000000001015
127 #use this element (change to 1 or 2 to see the effect on the scale and label)
129 #get the multiplier from the value at iIndex
130 iMultiplier=get_multiplier(scaleValues[iIndex])
131 print '\nScale example'
134 for aValue in scaleValues: print decimalFormat(aValue/iMultiplier, iDecimals),
135 #print the scale label using the value at iIndex
136 print '\n'+get_prefix(scaleValues[iIndex])+'N'