【发布时间】:2013-10-29 20:55:23
【问题描述】:
您好,请您参考下面的示例代码,让我知道 round 和 int 函数之间的确切区别是什么 输出: 12 美元和 0 美分 12 美元和 1.0 美分
# code is to convert xx.yy to xx dollars and yy cents **
val = 12.01
dollars = int(val) #Integer value to know how many Dollars
cents = int(100 * (val - dollars)) #Integer value to know how many cents
print str(dollars) + " dollars and " + str(cents) + " cents"
如果我用圆形函数编写相同的代码,我会得到正确的答案
val = 12.01
dollars = int(val) #Integer value to know how many Dollars
cents = round(100 * (val - dollars)) #Integer value to know how many cents
print str(dollars) + " dollars and " + str(cents) + " cents"
不知道为什么当我使用 int 时它显示为 0 美分。
【问题讨论】:
-
这是我第一次使用stackoverflow。请忽略格式错误。下面是使用 ROUND 函数的代码:
标签: python