我是 Python 的新手和一名学生,我的大学为我们的类(class)选择了地球上最糟糕的书。我找不到任何概念的例子,所以我提前道歉,因为我知道这些概念非常基础。我希望你能帮助我。
我需要知道如何在字符串中使用舍入功能。我找到了例子,但它们没有显示字符串,只是简单的数字。
这是我们应该得到的输出: 输入总收入:12345.67 输入家属人数:1
所得税是 $-130.87 <---- 这就是我们应该计算出来的
这是我们要更改的代码:
TAX_RATE = 0.20
STANDARD_DEDUCTION = 10000.0
DEPENDENT_DEDUCTION = 3000.0
# Request the inputs
grossIncome = float(input("Enter the gross income: "))
numDependents = int(input("Enter the number of dependents: "))
# Compute the income tax
taxableIncome = grossIncome - STANDARD_DEDUCTION - \
DEPENDENT_DEDUCTION * numDependents
incomeTax = taxableIncome * TAX_RATE
# Display the income tax
print("The income tax is $" + str(incomeTax))
因为我没有数字可以插入到公式中 - 我必须弄清楚如何使用“incomeTax” - 我不知道该怎么做。书中没有解释。帮忙?
最佳答案
您可以使用格式字符串:
print("The income tax is ${:.2f}".format(incomeTax))
如果您使用的是 Python 3.6+,您还可以使用 f-strings:
print(f"The income tax is ${incomeTax:.2f}")
关于python - 你如何在 Python 中舍入一个字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54384570/