百分比计算器
计算百分比、变化率和小费金额。
百分比计算器在单一易用界面中处理最常见的百分比问题:找出一个数是另一个数的百分之几、计算给定值的百分比、在已知某值的百分比时求原始值、以及计算两个数之间的百分比增减。
只需选择所需的计算类型,输入已知值,结果立即显示。您也可以反向计算——例如,如果您知道折扣价和折扣百分比,该工具可以告诉您原始价格。
百分比计算随处可见:在零售折扣和加价、税务计算、财务回报、成绩计算、小费计算和统计分析中。此工具消除了算术错误,为所有这些日常场景提供精确结果。
常见问题
代码实现
def percentage_of(percent, total):
"""X% of Y"""
return total * percent / 100
def what_percent(part, total):
"""X is what % of Y"""
return (part / total) * 100 if total != 0 else 0
def percent_change(old_value, new_value):
"""Percentage change from old to new"""
return ((new_value - old_value) / old_value) * 100 if old_value != 0 else 0
def add_percent(value, percent):
"""Add X% to a value"""
return value * (1 + percent / 100)
def subtract_percent(value, percent):
"""Subtract X% from a value"""
return value * (1 - percent / 100)
# Examples
print(f"15% of 200: {percentage_of(15, 200)}")
print(f"30 is what % of 120: {what_percent(30, 120):.2f}%")
print(f"50 -> 75 change: {percent_change(50, 75):.2f}%")
print(f"100 + 20%: {add_percent(100, 20)}")
print(f"100 - 15%: {subtract_percent(100, 15)}")Comments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.