【问题标题】:Add percentage to value (python)将百分比添加到值(python)
【发布时间】:2021-02-22 11:33:13
【问题描述】:

我有代码将数据写入 3 个不同的文件。我需要在 f3 的价格输出中添加销售税,这将改变价格值。最干净的方法是什么?

编辑:价格结构为 (00.00)

 # write compiled product to buylist.txt
def write_output(self):
    with open(self.outputfile, 'w') as f, open(self.outputfile2, 'w') as f2, open(self.outputfile3, 'w') as f3:
        for item in self.buylist:
            f.write(str(item['status']) + " " + str(item['item']) + " " + str(item['quantity'])+ " " + str(item['price']) + "\n")
            f2.write(str(item['item']) + " " + str(item['quantity']) + "\n")
            f3.write(str(item['item']) + " " + str(item['quantity'])+ " " + str(item['price']) + "\n")
    f.close(), f2.close(), f3.close()

【问题讨论】:

    标签: python add percentage


    【解决方案1】:

    你可以使用这个代码,虽然这不是一个很好的提问方式,因为你没有提到价格的数据类型或给出一个例子。

    您只需要编辑f3.write

    salesTax = 18
    f3.write(str(item['item']) + " " + str(item['quantity'])+ " " + str(int(item['price']) * (1 + (salesTax/100))) + "\n")
    

    【讨论】:

    • 你是对的。数据类型/价格结构是标准的 2 位小数 (00.00)
    • 谢谢!它工作得很好。请注意,括号太多了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多