未設定資料格式前:
import localenum = 123456789print(locale.format("%.2f", a, 1)) Output:
123456789.00
設定資料輸出格式:
locale.setlocale(locale.LC_ALL, '') # Set the locale for your system
Output:
"Chinese (Simplified)_People's Republic of China.936"
設定資料格式後:
print(locale.format("%.2f", num, 1)) Output:
123,456,789.00
函數介紹:
locale. setlocale ( category [, locale ] )
If locale is given and not None,setlocale() modifies the locale setting for thecategory. The available categories are listed in the data description below.locale may be a string, or an iterable of two strings (language code and encoding). If it’s an iterable, it’s converted to a locale name using the locale aliasing engine. An empty string specifies the user’s default settings. If the modification of the locale fails, the exception Error is raised. If successful, the new locale setting is returned.
If locale is omitted or None, the current setting forcategory is returned.
locale. format ( format, val [, grouping [, monetary ] ] )
Formats a number val according to the current LC_NUMERIC setting. The format follows the conventions of the% operator. For floating point values, the decimal point is modified if appropriate. Ifgrouping is true, also takes the grouping into account.
If monetary is true, the conversion uses monetary thousands separator and grouping strings.