简体   繁体   English

将浮点数扩展到多余的小数位

[英]Extending A Floating Point Number to Extra Decimal Places

So, in my beginner python class, we were asked to perform some basic arithmetic on numbers typed by the user, basic stuff. 因此,在我的初学者python类中,我们被要求对用户键入的数字(基本内容)执行一些基本的算术运算。 It was supposed to return a number with 2 decimal places, but I can only get it to extend to one. 本来应该返回一个小数点后两位的数字,但是我只能将其扩展为一个。 If the decimal terminates at the first decimal, is there a way to add another place? 如果小数点在第一个小数点处终止,是否可以添加另一个位置? If the decimal terminates later, or is infinite, is there a way to display the second number as well? 如果小数点以后终止或为无穷小,是否也可以显示第二个数字?

    print "Please enter the month of your birthday as a number.";
    print "\t Jan = 1";
    print "\t Feb = 2";
    print "\t Apr = 3"
    print "etc, etc, etc."
    num = input("\n");
    day = input("What date were you born on?\n");
    new = (((((((((((num * 7.0) - 1.0) * 13.0) + day) + 3.0) * 11.0) - num) - day) / 10.0) + 11.00) / 100.0);
    print "You were born in the", new, "month.";

Just had to experiment for a little bit, and my solution is kind of cheating. 只是需要做一点实验,我的解决方案有点作弊。

    new = str(new) + "0";

This changes the "new" into a string, and adds an extra 0 to the float. 这会将“ new”更改为字符串,并向浮点数添加额外的0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM