Format numbers as currency in Python

后端 未结 2 1776
攒了一身酷
攒了一身酷 2020-12-31 13:05

I learn from Currency formatting in Python, use the locale module to format numbers as currency. For instance,

#! /usr/bin/env python
# -*- coding: utf-8 -*-         


        
2条回答
  •  臣服心动
    2020-12-31 13:08

    For reference (for those that are looking to format numbers similar to how you would format currency), you can use locale.format_string to format numbers

    value = 123456789
    
    import locale
    locale.setlocale(locale.LC_ALL, 'de_DE') 
    print(locale.format_string('%.2f', value, True))
    

    Would return

    123.456.789,00
    

提交回复
热议问题