gpt4 book ai didi

Python - 查找美元和分

转载 作者:太空宇宙 更新时间:2023-11-03 14:46:37 25 4
gpt4 key购买 nike

假设价格是一个整数变量,其值为商品的价格(以美元为单位),以美分为单位。编写一条语句,将价格值以“X 美元和 Y 美分”的形式单独打印在一行上。因此,如果价格的值为 4321,您的代码将打印“43 美元 21 美分”。如果值为 501,它将打印“5 美元 1 美分”。如果值为 99,您的代码将打印“0 美元和 99 美分”。

我这样做了:

print (price/100,"dollars and",price%100, "cents")

结果例如:2314

23.14 dollars and 14 cents

如何使结果看起来像:

23 dollars and 14 cents

最佳答案

输入:

x = 4321

print (x/100),'dollars and',int(100*((x/100.00)-(x/100))),'cents'

输出:

43 dollars and 21 cents

关于Python - 查找美元和分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46189874/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com