单位

单位模块提供格式化不同地区的测量单位的功能。

babel.units.format_unit(value: str | float | decimal.Decimal, measurement_unit: str, length: Literal['short', 'long', 'narrow'] = 'long', format: str | None = None, locale: Locale | str | None = 'en_US', *, numbering_system: Literal['default'] | str = 'latn') str

格式化给定单位的值。

值根据区域设置的通常复数规则和数字格式进行格式化。

>>> format_unit(12, 'length-meter', locale='ro_RO')
u'12 metri'
>>> format_unit(15.5, 'length-mile', locale='fi_FI')
u'15,5 mailia'
>>> format_unit(1200, 'pressure-millimeter-ofhg', locale='nb')
u'1\xa0200 millimeter kvikks\xf8lv'
>>> format_unit(270, 'ton', locale='en')
u'270 tons'
>>> format_unit(1234.5, 'kilogram', locale='ar_EG', numbering_system='default')
u'1٬234٫5 كيلوغرام'

数字格式可以用 format 参数。

>>> import decimal
>>> format_unit(decimal.Decimal("-42.774"), 'temperature-celsius', 'short', format='#.0', locale='fr')
u'-42,8\u202f\xb0C'

尊重当地通常的多元化规则。

>>> format_unit(1, 'length-meter', locale='ro_RO')
u'1 metru'
>>> format_unit(0, 'length-mile', locale='cy')
u'0 mi'
>>> format_unit(1, 'length-mile', locale='cy')
u'1 filltir'
>>> format_unit(3, 'length-mile', locale='cy')
u'3 milltir'
>>> format_unit(15, 'length-horse', locale='fi')
Traceback (most recent call last):
    ...
UnknownUnitError: length-horse is not a known unit in fi

在 2.2.0 版本加入.

参数:
  • value -- 要格式化的值。如果这是字符串,则不会尝试设置数字格式。

  • measurement_unit -- 度量单位的代码。已知的单位可以在CLDR单位有效性XML文件中找到:https://unicode.org/repos/cldr/tags/latest/common/validity/unit.xml

  • length -- “短”、“长”或“窄”

  • format -- 可选格式,由接受 format_decimal .

  • locale -- 这个 Locale 对象或区域设置标识符

  • numbering_system -- 用于格式化数字符号的编号系统。默认为“latn”。特殊值“Default”将使用区域设置的默认编号系统。

抛出:

UnsupportedNumberingSystemError -- 如果区域设置不支持编号系统。

babel.units.format_compound_unit(numerator_value: str | float | decimal.Decimal, numerator_unit: str | None = None, denominator_value: str | float | decimal.Decimal = 1, denominator_unit: str | None = None, length: Literal['short', 'long', 'narrow'] = 'long', format: str | None = None, locale: Locale | str | None = 'en_US', *, numbering_system: Literal['default'] | str = 'latn') str | None

设置复合数字值的格式,即“公里/小时”或类似的值。

这两个单位说明符都是可选的,以允许仍根据区域设置的通用“per”格式说明符对任意值进行格式化。

>>> format_compound_unit(7, denominator_value=11, length="short", locale="pt")
'7/11'
>>> format_compound_unit(150, "kilometer", denominator_unit="hour", locale="sv")
'150 kilometer per timme'
>>> format_compound_unit(150, "kilowatt", denominator_unit="year", locale="fi")
'150 kilowattia / vuosi'
>>> format_compound_unit(32.5, "ton", 15, denominator_unit="hour", locale="en")
'32.5 tons per 15 hours'
>>> format_compound_unit(1234.5, "ton", 15, denominator_unit="hour", locale="ar_EG", numbering_system="arab")
'1٬234٫5 طن لكل 15 ساعة'
>>> format_compound_unit(160, denominator_unit="square-meter", locale="fr")
'160 par m\xe8tre carr\xe9'
>>> format_compound_unit(4, "meter", "ratakisko", length="short", locale="fi")
'4 m/ratakisko'
>>> format_compound_unit(35, "minute", denominator_unit="fathom", locale="sv")
'35 minuter per famn'
>>> from babel.numbers import format_currency
>>> format_compound_unit(format_currency(35, "JPY", locale="de"), denominator_unit="liter", locale="de")
'35\xa0\xa5 pro Liter'

请参阅https://www.unicode.org/reports/tr35/tr35-general.html#perUnitPatterns

参数:
  • numerator_value -- 分子的值。这可以是一个字符串,在这种情况下,它被认为是预格式化的,并且忽略该单位。

  • numerator_unit -- 分子单位。看见 format_unit .

  • denominator_value -- 分母的值。这可以是一个字符串,在这种情况下,它被认为是预格式化的,并且忽略该单位。

  • denominator_unit -- 分母单位。看见 format_unit .

  • length -- 格式长度。“短”、“长”或“窄”

  • format -- 可选格式,由接受 format_decimal .

  • locale -- 这个 Locale 对象或区域设置标识符

  • numbering_system -- 用于格式化数字符号的编号系统。默认为“latn”。特殊值“Default”将使用区域设置的默认编号系统。

返回:

格式化的复合值。

抛出:

UnsupportedNumberingSystemError -- 如果区域设置不支持编号系统。

babel.units.get_unit_name(measurement_unit: str, length: Literal['short', 'long', 'narrow'] = 'long', locale: Locale | str | None = 'en_US') str | None

获取给定区域设置中度量单位的显示名称。

>>> get_unit_name("radian", locale="en")
'radians'

未知单位将引发异常:

>>> get_unit_name("battery", locale="fi")
Traceback (most recent call last):
    ...
UnknownUnitError: battery/long is not a known unit/length in fi
参数:
返回:

设备显示名称,或无。