简体   繁体   English

如何在 python 中将浮点数格式化为字符串?

[英]How can I format a float as a string in python?

def main():
    M = float(input('Please enter sales for Monday: '))
    T = float(input('Please enter sales for Tuesday: '))
    W = float(input('Please enter sales for Wednesday: '))
    R = float(input('Please enter sales for Thursday: '))
    F = float(input('Please enter sales for Friday: '))
    sales = [M, T, W, R, F]

        total = 0 

    for value in sales:
        total += value

    print ('The total sales for the week are: $',total('.2f'))

main()

With the .2f format I am getting this exception:使用.2f格式,我得到了这个异常:

TypeError: 'float' object is not callable

If I remove the .2f the script runs properly but the formatting is not how I would like it, it displays as:如果我删除.2f脚本运行正常但格式不是我想要的,它显示为:

The total sales for the week are: $ 2500.0

I would prefer to have it so that there are 2 decimal places and no space between the $ sign.我希望它有 2 个小数位,并且 $ 符号之间没有空格。

Still new to python and learning the basics. python 和学习基础知识仍然是新手。 Your help is greatly appreciated.非常感谢您的帮助。

here's a solution using python's 3 f-string feature这是使用 python 的 3 f-string 功能的解决方案

print (f'The total sales for the week are: {total:.2f}')

In python, you can format strings in a variety of ways.在 python 中,您可以通过多种方式格式化字符串。 Here are some good resources on them:这里有一些关于它们的好资源:

For your case, you can format the total value like this:对于您的情况,您可以像这样格式化总值:

>>> total = 1234.56789

>>> "{:.2f}".format(total)
'1234.57'

>>> "%.2f" % total
'1234.57'

# This only works in 3.7 and later
>>> f"{total:.2f}"
'1234.57'

For your particular case, you can format the entire print string in one go:对于您的特定情况,您可以在一个 go 中格式化整个print字符串:

print(f"The total sales for the week are: ${total:.2f}")

replace代替

print ('The total sales for the week are: $',total('.2f'))

for为了

print ('The total sales for the week are: $',"{0:.2f}".format(total))

You may total a list with the builtin sum function.您可以使用内置sum function 汇总一个列表。 Try print(sum(sales)) .试试print(sum(sales))

You may format your floating point like this print(f'Week is {sum(sales):.2f}')您可以像这样格式化浮点数print(f'Week is {sum(sales):.2f}')

Small nits.小尼特。

Keep Hacking.继续黑客攻击。 Keep notes.记笔记。

Formatters in Python allow you to use curly braces as placeholders for values that you'll pass through with the str.format() method. Python 中的格式化程序允许您使用花括号作为您将通过str.format()方法传递的值的占位符。

As per your requirement根据您的要求

total = 0 
print ('The total sales for the week are: $',"{0:.2f}".format(total))

check here Limiting floats to two decimal points在这里检查将浮点数限制为小数点后两位

sample code示例代码

def main():
    M = float(input('Please enter sales for Monday: '))
    T = float(input('Please enter sales for Tuesday: '))
    W = float(input('Please enter sales for Wednesday: '))
    R = float(input('Please enter sales for Thursday: '))
    F = float(input('Please enter sales for Friday: '))
    sales = [M, T, W, R, F]

    total = 0 

    for value in sales:
        total += value
    total = "{0:.2f}".format(total)
    print ('The total sales for the week are: $' + str(total))

main()

Just make this correction:只需进行此更正:

print('The total sales for the week are:',format(total,('.2f')),'$')

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

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