[英]How to seperate last two digits with dot in Python
I always want to separate the last two digits with a decimal point . 我总是想用小数点分隔后两位。 (dot)L
(点)L
print("{:.2f}".format(300));
Output: 输出:
300.00
Expected output: 预期产量:
3.00
Next 下一个
print("{:.2f}".format(333333300));
Output: 输出:
333333300.00
Expected output: 预期产量:
3333333.00
How can I change my code so that I get the expected output instead? 如何更改代码,以便获得预期的输出?
300.00
is a representation of number 300
. 300.00
是数字300
的表示。 3.00
is a representation of number 3
. 3.00
是数字3
的表示。 Those are two different values. 那是两个不同的值。 There is no way Python will represent
300
as 3
. Python无法将
300
表示为3
。
Assuming you really want to print 3.00
(which for pretty much everyone around means 3
, not 300
), then you should divide 300
by 100
. 假设您确实要打印
3.00
(对于周围的每个人来说,这意味着3
,而不是300
),那么应该将300
除以100
。
print("{:.2f}".format(300/100.0))
Use the number value you actually want to output, ie don't add the '0' you want in the string representation. 使用您实际要输出的数字值,即不要在字符串表示形式中添加所需的“ 0”。
print("{:.2f}".format(3))
It is the string formatting which does add the '0' for you when it creates the string representation of the number value, depending on the specified formatting string. 字符串格式会在创建数字值的字符串表示形式时为您添加“ 0”,具体取决于指定的格式字符串。
def f(n):
a = divmod(n,100)
if a[1] < 10:
print("{}.0{}".format(a[0],a[1]))
else:
print("{}.{}".format(a[0],a[1]))
In Python 2.6 : 在Python 2.6中:
def myPrint(x):
print("{0:.2f}".format(x/100.00));
myPrint(2990);
29.90 29.90
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.