首页 > 其他 > 详细

关于float,赋值

时间:2015-11-30 18:13:29      阅读:475      评论:0      收藏:0      [点我收藏+]

阿萨姆奶茶一箱15支装。

在http://www.ds365.com/                      的价格是44

在http://www.synergyiclub.com:8086/          的价格是45

外面零售是4.5一支


在计算对比是,44/15 的结果变成了2,实际应该是2.9333333

WHY?

代码:

-----------------------------------



box_ds = 44

box_syn = 45

num_in_box = 15

per_sale_retail_price = 4


print "Here is the incorrect method:"

per_ds_incorrect = box_ds / num_in_box #Why did it calculate incorrectly here?

print "See what will happen now:"

per_ds = float(box_ds) / float(num_in_box)


per_syn = box_syn / num_in_box

print per_ds

print per_ds_incorrect

print "The num what I want is %f" % per_ds

print "The num what I don‘t want is %r " % per_ds_incorrect

print "The price of a bottle Asamu Milktea  on ds is : %r" % per_ds

print "The price of a bottle Asamu Milktea on Synergy is : %s" % per_syn

print "The price of a bottle Asamu Milktea on retail is : %r" % per_sale_retail_price


---------------------------------------


总结:必须使用浮点型,但是以下是错的:

float(per_ds_incorrect = box_ds / num_in_box)

float(per_ds_incorrect)= box_ds / num_in_box

per_ds_incorrect)= float(box_ds / num_in_box

正确的是:

per_ds = float(box_ds) / float(num_in_box)







本文出自 “StudyLog” 博客,谢绝转载!

关于float,赋值

原文:http://warrenmilo.blog.51cto.com/5533075/1718160

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!