在 Python 中计算两个数字之间的百分比
要计算两个数字之间的百分比,请将一个数字除以另一个数字,然后将结果乘以 100,例如 (30 / 75) * 100
。这显示第一个数字占第二个数字的百分比。 在示例中,30 是 75 的 **40%**。
def is_what_percent_of(num_a, num_b):
return (num_a / num_b) * 100
print(is_what_percent_of(30, 75)) # 👉️ 40.0
print(is_what_percent_of(20, 75)) # 👉️ 26.6666...
print(round(is_what_percent_of(20, 75), 2)) # 👉️ 26.67
# --------------------------------------------------
def get_percentage_increase(num_a, num_b):
return ((num_a - num_b) / num_b) * 100
print(get_percentage_increase(50, 30)) # 👉️ 66.6666...
print(get_percentage_increase(50, 100)) # 👉️ -50.0
# --------------------------------------------------
def get_percentage_difference(num_a, num_b):
# 👇️ use abs() function to always get positive number
return (abs(num_a - num_b) / num_b) * 100
print(get_percentage_difference(50, 30)) # 👉️ 66.6666...
print(get_percentage_difference(50, 100)) # 👉️ 50.0
第一个函数接受 2 个数字并返回第一个数字占第二个数字的百分比。
例如,25 / 50 * 100
表示 25 是 50 的 **50%**。
print((25 / 50) * 100) # 👉️ 50.0
计算两个数字之间的百分比时,我们可能需要四舍五入到小数点后的特定位数。
round
函数采用以下 2 个参数:
-
number 要四舍五入到小数点后
ndigits
精度的数字 - ndigits 数字在操作后应具有的小数点后的位数(可选)
print(round((23 / 43) * 100, 2)) # 👉️ 53.49
round
函数返回四舍五入到小数点后ndigits
精度的数字。
如果省略 ndigits,函数返回最接近的整数。
第二个函数显示如何获得两个数字之间的百分比增加/减少。
def get_percentage_increase(num_a, num_b):
return ((num_a - num_b) / num_b) * 100
print(get_percentage_increase(50, 30)) # 👉️ 66.6666...
print(get_percentage_increase(50, 100)) # 👉️ -50.0
第一个示例显示从 30 增加到 50 的百分比是 66.6666...%
。
第二个显示从 100 到 50 的百分比增加是 -50%
。
如果你总是需要得到一个正数,使用 abs()
函数。
def get_percentage_increase(num_a, num_b):
return (abs(num_a - num_b) / num_b) * 100
print(get_percentage_increase(50, 30)) # 👉️ 66.6666...
print(get_percentage_increase(50, 100)) # 👉️ 50.0
abs
函数返回数字的绝对值。 换句话说,如果数字是正数,则返回数字,如果数字是负数,则返回数字的负数。这样,我们始终可以保证在计算两个数字之间的百分比差异时得到一个正数。
我们可能想要处理的事情是除以 0。除以 0 会在 Python 中引发
ZeroDivisionError
。
def get_percentage_increase(num_a, num_b):
return (abs(num_a - num_b) / num_b) * 100
print(get_percentage_increase(50, 0)) # 👉️ inf
print(get_percentage_increase(50, 50)) # 👉️ 0.0
print(get_percentage_increase(50, 100)) # 👉️ 50.0
我们可以在 try/except
块中处理错误。
def get_percentage_increase(num_a, num_b):
try:
return (abs(num_a - num_b) / num_b) * 100
except ZeroDivisionError:
return float('inf')
print(get_percentage_increase(50, 0)) # 👉️ inf
print(get_percentage_increase(50, 50)) # 👉️ 0.0
print(get_percentage_increase(50, 100)) # 👉️ 50.0
如果我们收到 ZeroDivisionError
错误,我们将返回 Infinity
,但是我们可以以适合您的用例的任何其他方式处理该错误。
相关文章
Python pandas.pivot_table() 函数
发布时间:2024/04/24 浏览次数:82 分类:Python
-
Python Pandas pivot_table()函数通过对数据进行汇总,避免了数据的重复。
在 Python 中将 Pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:Python
-
了解如何在 Python 中将 Pandas 系列日期时间转换为字符串
在 Python Pandas 中使用 str.split 将字符串拆分为两个列表列
发布时间:2024/04/24 浏览次数:1124 分类:Python
-
本教程介绍如何使用 pandas str.split() 函数将字符串拆分为两个列表列。
在 Pandas 中将 Timedelta 转换为 Int
发布时间:2024/04/23 浏览次数:231 分类:Python
-
可以使用 Pandas 中的 dt 属性将 timedelta 转换为整数。
Python 中的 Pandas 插入方法
发布时间:2024/04/23 浏览次数:112 分类:Python
-
本教程介绍了如何在 Pandas DataFrame 中使用 insert 方法在 DataFrame 中插入一列。
使用 Python 将 Pandas DataFrame 保存为 HTML
发布时间:2024/04/21 浏览次数:106 分类:Python
-
本教程演示如何将 Pandas DataFrame 转换为 Python 中的 HTML 表格。
如何将 Python 字典转换为 Pandas DataFrame
发布时间:2024/04/20 浏览次数:73 分类:Python
-
本教程演示如何将 python 字典转换为 Pandas DataFrame,例如使用 Pandas DataFrame 构造函数或 from_dict 方法。
如何在 Pandas 中将 DataFrame 列转换为日期时间
发布时间:2024/04/20 浏览次数:101 分类:Python
-
本文介绍如何将 Pandas DataFrame 列转换为 Python 日期时间。