简体   繁体   English

用 2 位小数格式化逗号 PYTHON

[英]Formatting commas with 2 decimals PYTHON

I am trying to format a number to have commas and two decimal points at the end of it like: 100,000.00我正在尝试格式化一个数字,使其末尾有逗号和两个小数点,例如:100,000.00

This is what I have tried, but it won't work because I can't use these number formats on each other because they require a number to be inputted into the format.这是我尝试过的,但它不起作用,因为我不能相互使用这些数字格式,因为它们需要将数字输入到格式中。

"{:,}".format("{:.2f}".format(grossPay[i]))

So if you have any ideas on how I can do that, I would greatly appreciate it:D因此,如果您对我如何做到这一点有任何想法,我将不胜感激:D

Format specifiers can be combined:可以组合格式说明符:

>>> "{:,.2f}".format(100000)
'100,000.00'

if you just want to format any number into commas for 1000 and 2 decimals, all you need to do in Python is:如果您只想将任何数字格式化为 1000 和 2 位小数的逗号,您在 Python 中需要做的就是:

"{:,.2f}".format(grossPay[i])

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

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