简体   繁体   English

如何在Python中用点号分隔最后两位数字

[英]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.

相关问题 python 中的正则表达式替换列值的最后两位数字,如果最后两位数字为 30,则将其替换为 50 - Regular expression in python to replace last two digits of a column value where if last two digits are 30 then replace it with 50 删除 Python 中 CSV 的整个价格列的最后 2 位数字,或者在最后两位数字之前添加一个小数? - Remove last 2 digits in entire Price column of a CSV in Python, or add in a decimal before the last two digits? 如何在opencv python中将8点分成两部分 - How to seperate 8 points into two parts in opencv python 两个或多个数字的单独数字打印 - seperate number prints for two or more digits Python 2.7,在字符串的最后两位数字前添加破折号 - Python 2.7, add a dash before the last two digits of a string integer 的最后 2 位数字? Python 3 - Last 2 digits of an integer? Python 3 使用python将二进制数字分隔为excel行 - Seperate binary digits to excel rows with python 如何从两个单独的列表中获取两个值以进行打印(Python) - How to get two values from two seperate lists to print (python) 如何在python中舍入到两位数以进行计时 - How to round to two digits in python for timing 如何删除整数类型列中的最后两位数? - How to remove last the two digits in a column that is of integer type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM