请参阅上面链接的图片。我有一份电子邮件报告,显示带有括号的负值。我需要使每个数字的最后一位数字彼此对齐。运行 SQL 语句时,使用 for 循环和 if 语句打印这些值。这些值在 SQL 语句运行时被打印。
如何让正值向左移动一次以与括号中数字的最后一位对齐?
以下是打印报告中的列的 for 循环:
m1_total=0
m3_total=0
m6_total=0
m12_total=0
for line in curs:
if (line[1]==0) or (line[1]==None): #skip if the M1 is 0
continue
m1=0
m3=0
m6=0
m12=0
if line[1]>0:
m1=line[1]
if line[2]>0:
m3=round(line[2]/3)
if line[3]>0:
m6=round(line[3]/6)
if line[4]>0:
m12=round(line[4]/12)
mc3=0
mc6=0
mc12=0
if m3 > 0:
mc3=round((m1/m3)*100-100,2)
if m6>0:
mc6=round((m3/m6)*100-100,2)
if m12 > 0:
mc12=round((m6/m12)*100-100,2)
outStr+=str(line[0]).ljust(13,'.')+round_dolls(m1,9)+round_dolls(m3,9)+round_dolls(m6,9)+round_dolls(m12,9)
outStr+=round_dolls(mc3,14,'d')+round_dolls(mc6,9,'d')+round_dolls(mc12,9,'d') + '<br>'
m1_total+=m1
m3_total+=m3
m6_total+=m6
m12_total+=m12
mc3_total=0
mc6_total=0
mc12_total=0
if m3_total > 0:
mc3_total=round((m1_total/m3_total)*100-100,2)
if m6_total>0:
mc6_total=round((m3_total/m6_total)*100-100,2)
if m12_total > 0:
mc12_total=round((m6_total/m12_total)*100-100,2)
if line[-1] != ')':
str(m12) = str(m12)[:-1]
最后两行是我尝试实现解决方案的地方,但没有成功。
以下是将负数放在括号中的方法:
def format_num(num,justLen,justWithChar='.',howManyDecimal=0,putComma='Y',minusFormat='-',zeroChar='0'):
if not num:
num = 0
if num == 0:
if zeroChar <> '0':
return zeroChar.rjust(justLen,justWithChar)
roundedNum = round(num, howManyDecimal) # rounded becomes float
if putComma=='Y': # put comma at thousand
numStr = '{:,}'.format(roundedNum) # fractional part is truncated to 5 decimal place
else:
numStr = str(roundedNum)
if howManyDecimal == 0:
numStr = numStr.rsplit('.')[0] # 1,234.99 -> ['1,234', '99']
else: # to pad with 0 ex) 4234.9 -> 4234.90
numStr=numStr.rsplit('.')[0] + '.' + numStr.rsplit('.')[1].ljust(howManyDecimal, '0')
if num < 0:
if minusFormat=='P': # change - sign to parenthesis format
numStr = numStr.replace('-', '(') + ')'
return numStr.rjust(justLen,justWithChar)
这是来自 round_dolls() 的代码:
def round_dolls(num, justLen, format='I', zeroChar='0'):
if format == 'Q': # quantity - no comma - 99999
return format_num(num, justLen, '.', 0, 'N','-',zeroChar)
elif format == 'I': # integer - 99,999
return format_num(num, justLen, '.', 0, 'Y','-',zeroChar)
elif format == 'F': # float wit 2 decimal places - 99,999.99
return format_num(num, justLen, '.', 2, 'Y','-',zeroChar)
elif format == 'D': # 99,999 negative number (99,999)
return format_num(num, justLen, '.', 0, 'Y','P',zeroChar)
elif format == 'd': # 99,999.99 negative number (99,999.99)
return format_num(num, justLen, '.', 2, 'Y','P',zeroChar)
elif format == 'P': # percentage
return format_num(num*100, justLen, '.', 0) + '%'
else:
return 'Format not specified'
最佳答案
更改:
if num < 0:
if minusFormat=='P': # change - sign to parenthesis format
numStr = numStr.replace('-', '(') + ')'
至:
if minusFormat == 'P':
if num < 0: # change - sign to parenthesis format
numStr = numStr.replace('-', '(') + ')'
else: # add extra character to positive values to line up with negative
numStr = numStr + justWithChar
如果您不想要 .
在该行的最后一个字符中,您可以将其放在 for c in range(len(line))
之后如果不是 )
则循环删除该字符:
if rowStr[-1] != ')':
rowStr = rowStr[:-1]
关于python - 如何在 python 中使用 SQL 时移动值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44398471/