Python | 除法
1.除法
- 除 /
- 整除 //
- 求余 %
- 商和余数的元组 divmod
a = 9 b = 2 r1 = a/b print(r1) # 4.5 r2 = a//b print(r2) # 4 r3 = a%b print(r3) # 1 r4 = divmod(a,b) print(r4) # (4, 1) # 保留2位小数 r5 = round(a/b,2)
2.输出百分比
方式1:直接使用参数格式化:{:.2%}
{:.2%}
: 显示小数点后2位
显示小数点后2位:
print('percent: {:.2%}'.format(42/50))
不显示小数位:
{:.0%}
,即,将2
改为0
:
print('percent: {:.0%}'.format(42/50))
方式2:格式化为float,然后处理成%格式: {:.2f}%
与方式1的区别是:
(1) 需要对42/50
乘以 100 。
(2) 方式2的%
在{ }
外边,方式1的%
在{ }
里边。
#显示小数点后2位: print('percent: {:.2f}%'.format(42/50*100)) #显示小数点后1位: print('percent: {:.1f}%'.format(42/50*100)) #只显示整数位: #print('percent: {:.0f}%'.format(42/50*100))
a = 3 b = 11 percent = "%.2f%%" % (float(a)/float(b)*100) print(percent) # '27.27%' percent = "{:.2%}".format(float(a)/float(b)) print(percent) # '27.27%'
#默认顺序: print('percent1: {:.2%}, percent2: {:.1%}'.format(42/50, 42/100)) percent1: 84.00%, percent2: 42.0% #指定顺序:{1:.1%}对应第2个参数; {0:.1%}对应第1个参数。 print('percent2: {1:.1%}, percent1: {0:.1%}'.format(42/50, 42/100)) percent2: 42.0%, percent1: 84.0%
float(a)/float(b)
a = 3 b = 11 r = float(a)/float(b) print(r) # 0.2727272727272727
python实现小数精确四舍五入
一、现象描述
由于 python3 包括python2.7 以后的round策略使用的是decimal.ROUND_HALF_EVEN。即Round to nearest with ties going to nearest even integer. 也就是只有在整数部分是奇数的时候, 小数部分才逢5进1; 偶数时逢5舍去。 这有利于更好地保证数据的精确性, 并在实验数据处理中广为使用。
二、修改decimal上线文,实现一般意义上的精确四舍五入
ROUND_HALF_UP (to nearest with ties going away from zero), or
import decimal from decimal import Decimal print(decimal.getcontext()) context=decimal.getcontext() # 获取decimal现在的上下文 context.rounding = decimal.ROUND_HALF_UP #修改rounding策略 print(decimal.getcontext()) print(round(Decimal(1.5659898), 2)) print(round(Decimal(1.5344898), 2)) print(round(Decimal(2.5659898), 2)) print(round(Decimal(2.5359898), 2)) # with decimal.localcontext() as ctx: # ctx.prec = 32 # ctx.rounding=decimal.ROUND_HALF_UP # print(round(decimal.Decimal(2.669), 2)) # print(round(decimal.Decimal(1.635), 2))
运行结果:
Context(prec=28, rounding=ROUND_HALF_EVEN, Emin=-999999, Emax=999999, capitals=1, clamp=0, flags=[], traps=[InvalidOperation, DivisionByZero, Overflow]) 1.57 1.53 2.57 2.54 Context(prec=28, rounding=ROUND_HALF_UP, Emin=-999999, Emax=999999, capitals=1, clamp=0, flags=[Inexact, FloatOperation, Rounded], traps=[InvalidOperation, DivisionByZero, Overflow])
三、自定义四舍五入方法实现精确四舍五入
def round_up(number,power=0): """ 实现精确四舍五入,包含正、负小数多种场景 :param number: 需要四舍五入的小数 :param power: 四舍五入位数,支持0-∞ :return: 返回四舍五入后的结果 """ digit = 10 ** power num2 = float(int(number * digit)) # 处理正数,power不为0的情况 if number>=0 and power !=0: tag = number * digit - num2 + 1 / (digit * 10) if tag>=0.5: return (num2+1)/digit else: return num2/digit # 处理正数,power为0取整的情况 elif number>=0 and power==0 : tag = number * digit - int(number) if tag >= 0.5: return (num2 + 1) / digit else: return num2 / digit # 处理负数,power为0取整的情况 elif power==0 and number<0: tag = number * digit - int(number) if tag <= -0.5: return (num2 - 1) / digit else: return num2 / digit # 处理负数,power不为0的情况 else: tag = number * digit - num2 - 1 / (digit * 10) if tag <= -0.5: return (num2-1)/digit else: return num2/digit print(round_up( 2.4259, 0)) print(round_up( -2.625, 2)) print(round_up( -2.635, 2)) print(round_up( 0, 0))
结果示例:
2.0 -2.63 -2.64 0.0
单位换算
# 把带单位的转化为不带单位 def trans_genome_size(size,unit): if unit=="Gb": genome_size = int(float(size.strip())*(10**9)) elif unit=="Mb": genome_size = int(float(size.strip())*(10**6)) elif unit=="Kb": genome_size = int(float(size.strip())*(10**3)) else: genome_size = int(size.strip()) return genome_size # 把不带单位的转化为带单位 def trans_with_unit(size): size = int(size)*1.00 if size>10**9: genome_size = repr(round(size/10**9,2))+"Gb" elif size>10**6: genome_size = repr(round(size/10**6,2))+"Mb" elif size>10**3: genome_size = repr(round(size/10**3,2))+"Kb" print(size,genome_size) return genome_size # 转化为带逗号的 def trans_size_with_comma(data): data = str(int(data)) s = [] for n in range(1,len(data)+1): if n%3==0 and n!=len(data) : s.append(","+data[-n]) else: s.append(data[-n]) return "".join(s[::-1])