python十进制上下文输出

时间:2018-05-22 04:15:23

标签: python decimal

我想用某些格式输出某些十进制变量,我的理解是你用十进制类的上下文来做。我该怎么做呢?

$2,000.00

现在我想以{{1}}格式打印 我如何用上下文来做这件事?

3 个答案:

答案 0 :(得分:1)

val = 2000
print("${:.2f}".format(float(val)))   # This outputs $2000.00 (no comma)

但要添加到逗号,只需将它添加到冒号和小数之间,如下所示:

print("${:,.2f}".format(float(val)))   # This outputs $2,000.00 (with comma)
money = 65434562338.89
print("${:,.2f}".format(float(money))) # Outputs: $65,434,562,338.89

:.2f格式化输出,以便只显示2个小数位,:.3f将显示3个小数,如$2000.000,依此类推。

答案 1 :(得分:1)

尝试:

async deletePortfolio(portfolioId) {
    const deletePortfolio = await this.model.findOneAndDelete({
      _id: portfolioId,
    })
    return deletePortfolio
}

答案 2 :(得分:0)

试试export class DataBrokerService { public transactions:Transaction[]; constructor() {} ngOnInit(){ } getTransactions(): Observable<Transaction[]>{ if (needUpdate){ // Download from REST API, subscribe result to transactions this.updateTransactions(); } return of(this.transactions); } getTransaction(id:number): Observable <Transaction>{ let transaction:Transaction; this.getTransactions().subscribe(txns => transaction = txns.find(txn => txn.id === id)) }); return transaction$; } 模块

示例代码:

locale

输出:

import locale

x = 2000.00
locale.setlocale( locale.LC_ALL, '' )
locale.currency( x )
locale.currency( x, grouping=True )

参考:Currency formatting in Python

文档:https://docs.python.org/2/library/locale.html