Chapter XV: internationalization and localization -locale: Culture formatted digital localization API-

15.2.3 format numbers
in currency-neutral figures will be formatted differently depending on the locale. Specifically, there will be some number of packets (GROUPING) character for a large number of small partition readable.

import locale

sample_locales = [
    ('USA','en_US'),
    ('France','fr_FR'),
    ('Spain','es_ES'),
    ('Portugal','pt_PT'),
    ('Poland','pl_PL'),
    ]

print('{:>10} {:>10} {:>15}'.format(
    'Locale','Integer','Float')
      )
for name,loc in sample_locales:
    locale.setlocale(locale.LC_ALL,loc)

    print('{:>10}'.format(name),end=' ')
    print(locale.format('%10d',123456,grouping=True),end=' ')
    print(locale.firmat('%15.2f',123456.78,grouping=True))

To digital format without the currency may be the format () instead of currency ().

The take-digital converter into a digital format locale normalized (localized environment independent format), can delocalize ().

import locale

sample_locales = [
    ('USA','en_US'),
    ('France','fr_FR'),
    ('Spain','es_ES'),
    ('Portugal','pt_PT'),
    ('Poland','pl_PL'),
    ]

for name,loc in sample_locales:
    locale.setlocale(locale.LC_ALL,loc)
    localized = locale.format('%0.2f',123456.78,grouping=True)
    delocalized = locale.delocalize(localized)
    print('{:>10}: {:>10}  {:>10}'.format(
        name,
        localized,
        delocalized,
        ))

This will delete the packet symbols, additional hours separator is finally converted to a dot (.).

Guess you like

Origin blog.csdn.net/weixin_43193719/article/details/94832457