因此,此函数接收来自用户的输入并将其输入转换为值。例如,如果他们输入1k,输出将是1000.我希望能够倒退。所以说如果我的值为325000,我想将其改为325k。关于如何实现这一目标的任何想法?
class Parsing:
def __init__(self, instring):
self.instring = instring
def valueParsing(self):
self.instring = self.instring.strip()
self.parsedString = ''
self.scalerDict = {'K': 1000, 'MEG': 1000000, 'G': 1000000000, 'M': 0.001, 'U': 0.000001, 'N': 0.000000001, 'P': 0.000000000001}
self.scaler = 1.0
self.stringCounter = 0
self.errorflag = False
self.Parsedvalue = 0.0
self.inStringLength = len(self.instring)
for self.stringCounter in range (self.inStringLength):
if ((self.instring[self.stringCounter].upper()) == 'K'):
self.scaler = self.scalerDict['K']
elif ((self.instring[self.stringCounter].upper()) == 'G'):
self.scaler = self.scalerDict['G']
elif ((self.instring[self.stringCounter].upper()) == 'U'):
self.scaler = self.scalerDict['U']
elif ((self.instring[self.stringCounter].upper()) == 'N'):
self.scaler = self.scalerDict['N']
elif ((self.instring[self.stringCounter].upper()) == 'P'):
self.scaler = self.scalerDict['P']
elif ((self.instring[self.stringCounter].upper()) == 'M'):
if (((self.instring.upper()).count('MEG'))):
self.scaler = self.scalerDict['MEG']
else:
self.scaler = self.scalerDict['M']
elif (( self.instring[ self.stringCounter ].upper() ) == 'F' ):
break
elif (( self.instring[ self.stringCounter ].upper() ) == 'W' ):
break
elif (( self.instring[ self.stringCounter ].upper() ) == 'S' ):
break
elif (( self.instring[ self.stringCounter ].upper() ) == '%' ):
break
elif (( self.instring[ self.stringCounter ].upper() ) == 'V' ):
break
elif (( self.instring[ self.stringCounter ].upper() ) == 'A' ):
break
elif (( self.instring[ self.stringCounter ].upper() ) == 'H' ):
break
elif (( self.instring[ self.stringCounter ].upper() ) == 'Z' ):
break
elif (( self.instring[ self.stringCounter ]) == '.' ):
self.parsedString = self.parsedString + self.instring[ self.stringCounter ]
elif (self.instring[self.stringCounter].isdigit()):
if(int(self.instring[self.stringCounter]) >= 0):
if(int(self.instring[self.stringCounter]) <= 9):
self.parsedString = self.parsedString + self.instring[self.stringCounter]
else:
self.errorflag = True
break
else:
self.errorflag = True
print('Invalid input, Try again.')
if (self.errorflag):
self.parsedvalue = -1
else:
self.parsedvalue = long(self.parsedString)*self.scaler
return self.parsedvalue
print '1. Resistors in series\n',\
'2. Resistors in Parallel\n',\
'3. Voltage Divider\n'
iput = int(input("Enter your choice: "))
if iput == 1:
r1 = raw_input("Enter first resistor:")
r2 = raw_input("Enter second resistor:")
R1 = Parsing(r1)
R2 = Parsing(r2)
req = R1.valueParsing() + R2.valueParsing()
print "The value of the series resistors is %s." % req
答案 0 :(得分:1)
试试这个:
import math
def fmtnum(num):
k = (1e3, 'k')
M = (1e6, 'M')
G = (1e9, 'G')
table = {3: k, 4: k, 5: k, 6: M, 7: M, 8: M, 9: G, 10: G, 11: G}
num = float(num)
exp = math.log10(num)
if num < 0:
exp = int(exp)-1
else:
exp = int(exp)
try:
denum, suffix = table[exp]
return '{:g} {}'.format(num/denum, suffix)
except KeyError:
return '{:g}'.format(num)
将此解决方案扩展为10的负幂已经留给读者练习。 : - )
示例:
In [50]: fmtnum(3250)
Out[50]: '3.25 k'
In [51]: fmtnum(32500)
Out[51]: '32.5 k'
In [52]: fmtnum(325000)
Out[52]: '325 k'
In [53]: fmtnum(3250000)
Out[53]: '3.25 M'
In [54]: fmtnum(32500000)
Out[54]: '32.5 M'
In [55]: fmtnum(325000000)
Out[55]: '325 M'
In [56]: fmtnum(3250000000)
Out[56]: '3.25 G'
答案 1 :(得分:0)
怎么样:
n = 1000
for exp, name in zip(range(9, -13, -3), ('GMk1munp')):
if exp == 0:
continue
if isinstance(n, int):
if n % 10**exp == 0:
n = '{0:d}{1}'.format(n / 10**exp, name)
break
elif isinstance(n, basestring):
if n[-exp:] == '0' * exp:
n = '{0}{1}'.format(n[:-exp], name)
break
elif n[-1] == name:
n = n[:-1] + '0' * exp
break
向后运行指数,确保找到正确的匹配。