关于float,赋值

阿萨姆奶茶一箱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” 博客,谢绝转载!

你可能感兴趣的:(浮点型,阿萨姆奶茶)