| 1 |
# |
| 2 |
# Copyright (C) 2007-2011 Edgewall Software, 2013-2025 the Babel team |
| 3 |
# All rights reserved. |
| 4 |
# |
| 5 |
# This software is licensed as described in the file LICENSE, which |
| 6 |
# you should have received as part of this distribution. The terms |
| 7 |
# are also available at https://github.com/python-babel/babel/blob/master/LICENSE. |
| 8 |
# |
| 9 |
# This software consists of voluntary contributions made by many |
| 10 |
# individuals. For the exact contribution history, see the revision |
| 11 |
# history and logs, available at https://github.com/python-babel/babel/commits/master/. |
| 12 |
|
| 13 |
import decimal |
| 14 |
from datetime import date |
| 15 |
|
| 16 |
import pytest |
| 17 |
|
| 18 |
from babel import localedata, numbers |
| 19 |
from babel.numbers import ( |
| 20 |
UnknownCurrencyError, |
| 21 |
get_currency_precision, |
| 22 |
get_currency_unit_pattern, |
| 23 |
get_decimal_precision, |
| 24 |
is_currency, |
| 25 |
list_currencies, |
| 26 |
normalize_currency, |
| 27 |
validate_currency, |
| 28 |
) |
| 29 |
|
| 30 |
|
| 31 |
def test_list_currencies(): |
| 32 |
assert isinstance(list_currencies(), set) |
| 33 |
assert list_currencies().issuperset(['BAD', 'BAM', 'KRO']) |
| 34 |
|
| 35 |
assert isinstance(list_currencies(locale='fr'), set) |
| 36 |
assert list_currencies('fr').issuperset(['BAD', 'BAM', 'KRO']) |
| 37 |
|
| 38 |
with pytest.raises(ValueError, match="expected only letters, got 'yo!'"): |
| 39 |
list_currencies('yo!') |
| 40 |
|
| 41 |
assert list_currencies(locale='pa_Arab') == {'PKR', 'INR', 'EUR'} |
| 42 |
|
| 43 |
assert len(list_currencies()) == 308 |
| 44 |
|
| 45 |
|
| 46 |
def test_validate_currency(): |
| 47 |
validate_currency('EUR') |
| 48 |
|
| 49 |
with pytest.raises(UnknownCurrencyError, match="Unknown currency 'FUU'."): |
| 50 |
validate_currency('FUU') |
| 51 |
|
| 52 |
|
| 53 |
def test_is_currency(): |
| 54 |
assert is_currency('EUR') |
| 55 |
assert not is_currency('eUr') |
| 56 |
assert not is_currency('FUU') |
| 57 |
assert not is_currency('') |
| 58 |
assert not is_currency(None) |
| 59 |
assert not is_currency(' EUR ') |
| 60 |
assert not is_currency(' ') |
| 61 |
assert not is_currency([]) |
| 62 |
assert not is_currency(set()) |
| 63 |
|
| 64 |
|
| 65 |
def test_normalize_currency(): |
| 66 |
assert normalize_currency('EUR') == 'EUR' |
| 67 |
assert normalize_currency('eUr') == 'EUR' |
| 68 |
assert normalize_currency('FUU') is None |
| 69 |
assert normalize_currency('') is None |
| 70 |
assert normalize_currency(None) is None |
| 71 |
assert normalize_currency(' EUR ') is None |
| 72 |
assert normalize_currency(' ') is None |
| 73 |
assert normalize_currency([]) is None |
| 74 |
assert normalize_currency(set()) is None |
| 75 |
|
| 76 |
|
| 77 |
def test_get_currency_name(): |
| 78 |
assert numbers.get_currency_name('USD', locale='en_US') == 'US Dollar' |
| 79 |
assert numbers.get_currency_name('USD', count=2, locale='en_US') == 'US dollars' |
| 80 |
|
| 81 |
|
| 82 |
def test_get_currency_symbol(): |
| 83 |
assert numbers.get_currency_symbol('USD', 'en_US') == '$' |
| 84 |
|
| 85 |
|
| 86 |
def test_get_currency_precision(): |
| 87 |
assert get_currency_precision('EUR') == 2 |
| 88 |
assert get_currency_precision('JPY') == 0 |
| 89 |
|
| 90 |
|
| 91 |
def test_get_currency_unit_pattern(): |
| 92 |
assert get_currency_unit_pattern('USD', locale='en_US') == '{0} {1}' |
| 93 |
assert get_currency_unit_pattern('USD', locale='sw') == '{1} {0}' |
| 94 |
|
| 95 |
# 'ro' locale various pattern according to count |
| 96 |
assert get_currency_unit_pattern('USD', locale='ro', count=1) == '{0} {1}' |
| 97 |
assert get_currency_unit_pattern('USD', locale='ro', count=2) == '{0} {1}' |
| 98 |
assert get_currency_unit_pattern('USD', locale='ro', count=100) == '{0} de {1}' |
| 99 |
assert get_currency_unit_pattern('USD', locale='ro') == '{0} de {1}' |
| 100 |
|
| 101 |
|
| 102 |
def test_get_territory_currencies(): |
| 103 |
assert numbers.get_territory_currencies('AT', date(1995, 1, 1)) == ['ATS'] |
| 104 |
assert numbers.get_territory_currencies('AT', date(2011, 1, 1)) == ['EUR'] |
| 105 |
|
| 106 |
assert numbers.get_territory_currencies('US', date(2013, 1, 1)) == ['USD'] |
| 107 |
assert sorted(numbers.get_territory_currencies('US', date(2013, 1, 1), |
| 108 |
non_tender=True)) == ['USD', 'USN', 'USS'] |
| 109 |
|
| 110 |
assert numbers.get_territory_currencies('US', date(2013, 1, 1), |
| 111 |
include_details=True) == [{ |
| 112 |
'currency': 'USD', |
| 113 |
'from': date(1792, 1, 1), |
| 114 |
'to': None, |
| 115 |
'tender': True, |
| 116 |
}] |
| 117 |
|
| 118 |
assert numbers.get_territory_currencies('LS', date(2013, 1, 1)) == ['ZAR', 'LSL'] |
| 119 |
|
| 120 |
assert numbers.get_territory_currencies('QO', date(2013, 1, 1)) == [] |
| 121 |
|
| 122 |
# Croatia uses Euro starting in January 2023; this is in CLDR 42. |
| 123 |
# See https://github.com/python-babel/babel/issues/942 |
| 124 |
assert 'EUR' in numbers.get_territory_currencies('HR', date(2023, 1, 1)) |
| 125 |
|
| 126 |
|
| 127 |
def test_get_decimal_symbol(): |
| 128 |
assert numbers.get_decimal_symbol('en_US') == '.' |
| 129 |
assert numbers.get_decimal_symbol('en_US', numbering_system="default") == '.' |
| 130 |
assert numbers.get_decimal_symbol('en_US', numbering_system="latn") == '.' |
| 131 |
assert numbers.get_decimal_symbol('sv_SE') == ',' |
| 132 |
assert numbers.get_decimal_symbol('ar_EG') == '.' |
| 133 |
assert numbers.get_decimal_symbol('ar_EG', numbering_system="default") == '٫' |
| 134 |
assert numbers.get_decimal_symbol('ar_EG', numbering_system="latn") == '.' |
| 135 |
assert numbers.get_decimal_symbol('ar_EG', numbering_system="arab") == '٫' |
| 136 |
|
| 137 |
|
| 138 |
def test_get_plus_sign_symbol(): |
| 139 |
assert numbers.get_plus_sign_symbol('en_US') == '+' |
| 140 |
assert numbers.get_plus_sign_symbol('en_US', numbering_system="default") == '+' |
| 141 |
assert numbers.get_plus_sign_symbol('en_US', numbering_system="latn") == '+' |
| 142 |
assert numbers.get_plus_sign_symbol('ar_EG') == '\u200e+' |
| 143 |
assert numbers.get_plus_sign_symbol('ar_EG', numbering_system="default") == '\u061c+' |
| 144 |
assert numbers.get_plus_sign_symbol('ar_EG', numbering_system="arab") == '\u061c+' |
| 145 |
assert numbers.get_plus_sign_symbol('ar_EG', numbering_system="latn") == '\u200e+' |
| 146 |
|
| 147 |
|
| 148 |
def test_get_minus_sign_symbol(): |
| 149 |
assert numbers.get_minus_sign_symbol('en_US') == '-' |
| 150 |
assert numbers.get_minus_sign_symbol('en_US', numbering_system="default") == '-' |
| 151 |
assert numbers.get_minus_sign_symbol('en_US', numbering_system="latn") == '-' |
| 152 |
assert numbers.get_minus_sign_symbol('nl_NL') == '-' |
| 153 |
assert numbers.get_minus_sign_symbol('ar_EG') == '\u200e-' |
| 154 |
assert numbers.get_minus_sign_symbol('ar_EG', numbering_system="default") == '\u061c-' |
| 155 |
assert numbers.get_minus_sign_symbol('ar_EG', numbering_system="arab") == '\u061c-' |
| 156 |
assert numbers.get_minus_sign_symbol('ar_EG', numbering_system="latn") == '\u200e-' |
| 157 |
|
| 158 |
|
| 159 |
def test_get_exponential_symbol(): |
| 160 |
assert numbers.get_exponential_symbol('en_US') == 'E' |
| 161 |
assert numbers.get_exponential_symbol('en_US', numbering_system="latn") == 'E' |
| 162 |
assert numbers.get_exponential_symbol('en_US', numbering_system="default") == 'E' |
| 163 |
assert numbers.get_exponential_symbol('ja_JP') == 'E' |
| 164 |
assert numbers.get_exponential_symbol('ar_EG') == 'E' |
| 165 |
assert numbers.get_exponential_symbol('ar_EG', numbering_system="default") == 'أس' |
| 166 |
assert numbers.get_exponential_symbol('ar_EG', numbering_system="arab") == 'أس' |
| 167 |
assert numbers.get_exponential_symbol('ar_EG', numbering_system="latn") == 'E' |
| 168 |
|
| 169 |
|
| 170 |
def test_get_group_symbol(): |
| 171 |
assert numbers.get_group_symbol('en_US') == ',' |
| 172 |
assert numbers.get_group_symbol('en_US', numbering_system="latn") == ',' |
| 173 |
assert numbers.get_group_symbol('en_US', numbering_system="default") == ',' |
| 174 |
assert numbers.get_group_symbol('ar_EG') == ',' |
| 175 |
assert numbers.get_group_symbol('ar_EG', numbering_system="default") == '٬' |
| 176 |
assert numbers.get_group_symbol('ar_EG', numbering_system="arab") == '٬' |
| 177 |
assert numbers.get_group_symbol('ar_EG', numbering_system="latn") == ',' |
| 178 |
|
| 179 |
|
| 180 |
def test_get_infinity_symbol(): |
| 181 |
assert numbers.get_infinity_symbol('en_US') == '∞' |
| 182 |
assert numbers.get_infinity_symbol('ar_EG', numbering_system="latn") == '∞' |
| 183 |
assert numbers.get_infinity_symbol('ar_EG', numbering_system="default") == '∞' |
| 184 |
assert numbers.get_infinity_symbol('ar_EG', numbering_system="arab") == '∞' |
| 185 |
|
| 186 |
|
| 187 |
def test_decimal_precision(): |
| 188 |
assert get_decimal_precision(decimal.Decimal('0.110')) == 2 |
| 189 |
assert get_decimal_precision(decimal.Decimal('1.0')) == 0 |
| 190 |
assert get_decimal_precision(decimal.Decimal('10000')) == 0 |
| 191 |
|
| 192 |
|
| 193 |
def test_format_decimal(): |
| 194 |
assert numbers.format_decimal(1099, locale='en_US') == '1,099' |
| 195 |
assert numbers.format_decimal(1099, locale='de_DE') == '1.099' |
| 196 |
assert numbers.format_decimal(1.2345, locale='en_US') == '1.234' |
| 197 |
assert numbers.format_decimal(1.2346, locale='en_US') == '1.235' |
| 198 |
assert numbers.format_decimal(-1.2346, locale='en_US') == '-1.235' |
| 199 |
assert numbers.format_decimal(1.2345, locale='sv_SE') == '1,234' |
| 200 |
assert numbers.format_decimal(1.2345, locale='de') == '1,234' |
| 201 |
assert numbers.format_decimal(12345.5, locale='en_US') == '12,345.5' |
| 202 |
assert numbers.format_decimal(0001.2345000, locale='en_US') == '1.234' |
| 203 |
assert numbers.format_decimal(-0001.2346000, locale='en_US') == '-1.235' |
| 204 |
assert numbers.format_decimal(0000000.5, locale='en_US') == '0.5' |
| 205 |
assert numbers.format_decimal(000, locale='en_US') == '0' |
| 206 |
|
| 207 |
assert numbers.format_decimal(12345.5, locale='ar_EG') == '12,345.5' |
| 208 |
assert numbers.format_decimal(12345.5, locale='ar_EG', numbering_system="default") == '12٬345٫5' |
| 209 |
assert numbers.format_decimal(12345.5, locale='ar_EG', numbering_system="arab") == '12٬345٫5' |
| 210 |
|
| 211 |
with pytest.raises(numbers.UnsupportedNumberingSystemError): |
| 212 |
numbers.format_decimal(12345.5, locale='en_US', numbering_system="unknown") |
| 213 |
|
| 214 |
|
| 215 |
@pytest.mark.parametrize('input_value, expected_value', [ |
| 216 |
('10000', '10,000'), |
| 217 |
('1', '1'), |
| 218 |
('1.0', '1'), |
| 219 |
('1.1', '1.1'), |
| 220 |
('1.11', '1.11'), |
| 221 |
('1.110', '1.11'), |
| 222 |
('1.001', '1.001'), |
| 223 |
('1.00100', '1.001'), |
| 224 |
('01.00100', '1.001'), |
| 225 |
('101.00100', '101.001'), |
| 226 |
('00000', '0'), |
| 227 |
('0', '0'), |
| 228 |
('0.0', '0'), |
| 229 |
('0.1', '0.1'), |
| 230 |
('0.11', '0.11'), |
| 231 |
('0.110', '0.11'), |
| 232 |
('0.001', '0.001'), |
| 233 |
('0.00100', '0.001'), |
| 234 |
('00.00100', '0.001'), |
| 235 |
('000.00100', '0.001'), |
| 236 |
]) |
| 237 |
def test_format_decimal_precision(input_value, expected_value): |
| 238 |
# Test precision conservation. |
| 239 |
assert numbers.format_decimal( |
| 240 |
decimal.Decimal(input_value), locale='en_US', decimal_quantization=False) == expected_value |
| 241 |
|
| 242 |
|
| 243 |
def test_format_decimal_quantization(): |
| 244 |
# Test all locales. |
| 245 |
for locale_code in localedata.locale_identifiers(): |
| 246 |
assert numbers.format_decimal( |
| 247 |
'0.9999999999', locale=locale_code, decimal_quantization=False).endswith('9999999999') is True |
| 248 |
|
| 249 |
|
| 250 |
def test_format_currency(): |
| 251 |
assert (numbers.format_currency(1099.98, 'USD', locale='en_US') |
| 252 |
== '$1,099.98') |
| 253 |
assert (numbers.format_currency(1099.98, 'USD', locale='en_US', numbering_system="default") |
| 254 |
== '$1,099.98') |
| 255 |
assert (numbers.format_currency(0, 'USD', locale='en_US') |
| 256 |
== '$0.00') |
| 257 |
assert (numbers.format_currency(1099.98, 'USD', locale='es_CO') |
| 258 |
== 'US$1.099,98') |
| 259 |
assert (numbers.format_currency(1099.98, 'EUR', locale='de_DE') |
| 260 |
== '1.099,98\xa0\u20ac') |
| 261 |
assert (numbers.format_currency(1099.98, 'USD', locale='ar_EG', numbering_system="default") |
| 262 |
== '\u200f1٬099٫98\xa0US$') |
| 263 |
assert (numbers.format_currency(1099.98, 'EUR', '\xa4\xa4 #,##0.00', |
| 264 |
locale='en_US') |
| 265 |
== 'EUR 1,099.98') |
| 266 |
assert (numbers.format_currency(1099.98, 'EUR', locale='nl_NL') |
| 267 |
!= numbers.format_currency(-1099.98, 'EUR', locale='nl_NL')) |
| 268 |
assert (numbers.format_currency(1099.98, 'USD', format=None, |
| 269 |
locale='en_US') |
| 270 |
== '$1,099.98') |
| 271 |
assert (numbers.format_currency(1, 'USD', locale='es_AR') |
| 272 |
== 'US$1,00') # one |
| 273 |
assert (numbers.format_currency(1000000, 'USD', locale='es_AR') |
| 274 |
== 'US$1.000.000,00') # many |
| 275 |
assert (numbers.format_currency(0, 'USD', locale='es_AR') |
| 276 |
== 'US$0,00') # other |
| 277 |
|
| 278 |
|
| 279 |
def test_format_currency_format_type(): |
| 280 |
assert (numbers.format_currency(1099.98, 'USD', locale='en_US', |
| 281 |
format_type="standard") |
| 282 |
== '$1,099.98') |
| 283 |
assert (numbers.format_currency(0, 'USD', locale='en_US', |
| 284 |
format_type="standard") |
| 285 |
== '$0.00') |
| 286 |
|
| 287 |
assert (numbers.format_currency(1099.98, 'USD', locale='en_US', |
| 288 |
format_type="accounting") |
| 289 |
== '$1,099.98') |
| 290 |
assert (numbers.format_currency(0, 'USD', locale='en_US', |
| 291 |
format_type="accounting") |
| 292 |
== '$0.00') |
| 293 |
|
| 294 |
with pytest.raises(numbers.UnknownCurrencyFormatError, match="'unknown' is not a known currency format type"): |
| 295 |
numbers.format_currency(1099.98, 'USD', locale='en_US', format_type='unknown') |
| 296 |
|
| 297 |
assert (numbers.format_currency(1099.98, 'JPY', locale='en_US') |
| 298 |
== '\xa51,100') |
| 299 |
assert (numbers.format_currency(1099.98, 'COP', '#,##0.00', locale='es_ES') |
| 300 |
== '1.100') |
| 301 |
assert (numbers.format_currency(1099.98, 'JPY', locale='en_US', |
| 302 |
currency_digits=False) |
| 303 |
== '\xa51,099.98') |
| 304 |
assert (numbers.format_currency(1099.98, 'COP', '#,##0.00', locale='es_ES', |
| 305 |
currency_digits=False) |
| 306 |
== '1.099,98') |
| 307 |
|
| 308 |
|
| 309 |
def test_format_compact_currency(): |
| 310 |
assert numbers.format_compact_currency(1, 'USD', locale='en_US', format_type="short") == '$1' |
| 311 |
assert numbers.format_compact_currency(999, 'USD', locale='en_US', format_type="short") == '$999' |
| 312 |
assert numbers.format_compact_currency(123456789, 'USD', locale='en_US', format_type="short") == '$123M' |
| 313 |
assert numbers.format_compact_currency(123456789, 'USD', locale='en_US', fraction_digits=2, format_type="short") == '$123.46M' |
| 314 |
assert numbers.format_compact_currency(123456789, 'USD', locale='en_US', fraction_digits=2, format_type="short", numbering_system="default") == '$123.46M' |
| 315 |
assert numbers.format_compact_currency(-123456789, 'USD', locale='en_US', fraction_digits=2, format_type="short") == '-$123.46M' |
| 316 |
assert numbers.format_compact_currency(1, 'JPY', locale='ja_JP', format_type="short") == '¥1' |
| 317 |
assert numbers.format_compact_currency(1234, 'JPY', locale='ja_JP', format_type="short") == '¥1234' |
| 318 |
assert numbers.format_compact_currency(123456, 'JPY', locale='ja_JP', format_type="short") == '¥12万' |
| 319 |
assert numbers.format_compact_currency(123456, 'JPY', locale='ja_JP', format_type="short", fraction_digits=2) == '¥12.35万' |
| 320 |
assert numbers.format_compact_currency(123, 'EUR', locale='yav', format_type="short") == '€\xa0123' |
| 321 |
assert numbers.format_compact_currency(12345, 'EUR', locale='yav', format_type="short") == '€\xa012K' |
| 322 |
assert numbers.format_compact_currency(123456789, 'EUR', locale='de_DE', fraction_digits=1) == '123,5\xa0Mio.\xa0€' |
| 323 |
assert numbers.format_compact_currency(123456789, 'USD', locale='ar_EG', fraction_digits=2, format_type="short", numbering_system="default") == '\u200f123٫46\xa0مليون\xa0US$' |
| 324 |
|
| 325 |
|
| 326 |
def test_format_compact_currency_invalid_format_type(): |
| 327 |
with pytest.raises(numbers.UnknownCurrencyFormatError): |
| 328 |
numbers.format_compact_currency(1099.98, 'USD', locale='en_US', format_type='unknown') |
| 329 |
|
| 330 |
|
| 331 |
@pytest.mark.parametrize('input_value, expected_value', [ |
| 332 |
('10000', '$10,000.00'), |
| 333 |
('1', '$1.00'), |
| 334 |
('1.0', '$1.00'), |
| 335 |
('1.1', '$1.10'), |
| 336 |
('1.11', '$1.11'), |
| 337 |
('1.110', '$1.11'), |
| 338 |
('1.001', '$1.001'), |
| 339 |
('1.00100', '$1.001'), |
| 340 |
('01.00100', '$1.001'), |
| 341 |
('101.00100', '$101.001'), |
| 342 |
('00000', '$0.00'), |
| 343 |
('0', '$0.00'), |
| 344 |
('0.0', '$0.00'), |
| 345 |
('0.1', '$0.10'), |
| 346 |
('0.11', '$0.11'), |
| 347 |
('0.110', '$0.11'), |
| 348 |
('0.001', '$0.001'), |
| 349 |
('0.00100', '$0.001'), |
| 350 |
('00.00100', '$0.001'), |
| 351 |
('000.00100', '$0.001'), |
| 352 |
]) |
| 353 |
def test_format_currency_precision(input_value, expected_value): |
| 354 |
# Test precision conservation. |
| 355 |
assert numbers.format_currency( |
| 356 |
decimal.Decimal(input_value), |
| 357 |
currency='USD', |
| 358 |
locale='en_US', |
| 359 |
decimal_quantization=False, |
| 360 |
) == expected_value |
| 361 |
|
| 362 |
|
| 363 |
def test_format_currency_quantization(): |
| 364 |
# Test all locales. |
| 365 |
for locale_code in localedata.locale_identifiers(): |
| 366 |
assert numbers.format_currency( |
| 367 |
'0.9999999999', 'USD', locale=locale_code, decimal_quantization=False).find('9999999999') > -1 |
| 368 |
|
| 369 |
|
| 370 |
def test_format_currency_long_display_name(): |
| 371 |
assert (numbers.format_currency(1099.98, 'USD', locale='en_US', format_type='name') |
| 372 |
== '1,099.98 US dollars') |
| 373 |
assert (numbers.format_currency(1099.98, 'USD', locale='en_US', format_type='name', numbering_system="default") |
| 374 |
== '1,099.98 US dollars') |
| 375 |
assert (numbers.format_currency(1099.98, 'USD', locale='ar_EG', format_type='name', numbering_system="default") |
| 376 |
== '1٬099٫98 دولار أمريكي') |
| 377 |
assert (numbers.format_currency(1.00, 'USD', locale='en_US', format_type='name') |
| 378 |
== '1.00 US dollar') |
| 379 |
assert (numbers.format_currency(1.00, 'EUR', locale='en_US', format_type='name') |
| 380 |
== '1.00 euro') |
| 381 |
assert (numbers.format_currency(2, 'EUR', locale='en_US', format_type='name') |
| 382 |
== '2.00 euros') |
| 383 |
# This tests that '{1} {0}' unitPatterns are found: |
| 384 |
assert (numbers.format_currency(150, 'USD', locale='sw', format_type='name') |
| 385 |
== 'dola za Marekani 150.00') |
| 386 |
assert (numbers.format_currency(1, 'USD', locale='sw', format_type='name') |
| 387 |
== '1.00 dola ya Marekani') |
| 388 |
# This tests unicode chars: |
| 389 |
assert (numbers.format_currency(1099.98, 'USD', locale='es_GT', format_type='name') |
| 390 |
== '1,099.98 dólares estadounidenses') |
| 391 |
# Test for completely unknown currency, should fallback to currency code |
| 392 |
assert (numbers.format_currency(1099.98, 'XAB', locale='en_US', format_type='name') |
| 393 |
== '1,099.98 XAB') |
| 394 |
|
| 395 |
# Test for finding different unit patterns depending on count |
| 396 |
assert (numbers.format_currency(1, 'USD', locale='ro', format_type='name') |
| 397 |
== '1,00 dolar american') |
| 398 |
assert (numbers.format_currency(2, 'USD', locale='ro', format_type='name') |
| 399 |
== '2,00 dolari americani') |
| 400 |
assert (numbers.format_currency(100, 'USD', locale='ro', format_type='name') |
| 401 |
== '100,00 de dolari americani') |
| 402 |
|
| 403 |
|
| 404 |
def test_format_currency_long_display_name_all(): |
| 405 |
for locale_code in localedata.locale_identifiers(): |
| 406 |
assert numbers.format_currency( |
| 407 |
1, 'USD', locale=locale_code, format_type='name').find('1') > -1 |
| 408 |
assert numbers.format_currency( |
| 409 |
'1', 'USD', locale=locale_code, format_type='name').find('1') > -1 |
| 410 |
|
| 411 |
|
| 412 |
def test_format_currency_long_display_name_custom_format(): |
| 413 |
assert (numbers.format_currency(1099.98, 'USD', locale='en_US', |
| 414 |
format_type='name', format='##0') |
| 415 |
== '1099.98 US dollars') |
| 416 |
assert (numbers.format_currency(1099.98, 'USD', locale='en_US', |
| 417 |
format_type='name', format='##0', |
| 418 |
currency_digits=False) |
| 419 |
== '1100 US dollars') |
| 420 |
|
| 421 |
|
| 422 |
def test_format_percent(): |
| 423 |
assert numbers.format_percent(0.34, locale='en_US') == '34%' |
| 424 |
assert numbers.format_percent(0.34, locale='en_US', numbering_system="default") == '34%' |
| 425 |
assert numbers.format_percent(0, locale='en_US') == '0%' |
| 426 |
assert numbers.format_percent(0.34, '##0%', locale='en_US') == '34%' |
| 427 |
assert numbers.format_percent(34, '##0', locale='en_US') == '34' |
| 428 |
assert numbers.format_percent(25.1234, locale='en_US') == '2,512%' |
| 429 |
assert (numbers.format_percent(25.1234, locale='sv_SE') |
| 430 |
== '2\xa0512\xa0%') |
| 431 |
assert (numbers.format_percent(25.1234, '#,##0\u2030', locale='en_US') |
| 432 |
== '25,123\u2030') |
| 433 |
assert numbers.format_percent(134.5, locale='ar_EG', numbering_system="default") == '13٬450%' |
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
@pytest.mark.parametrize('input_value, expected_value', [ |
| 438 |
('100', '10,000%'), |
| 439 |
('0.01', '1%'), |
| 440 |
('0.010', '1%'), |
| 441 |
('0.011', '1.1%'), |
| 442 |
('0.0111', '1.11%'), |
| 443 |
('0.01110', '1.11%'), |
| 444 |
('0.01001', '1.001%'), |
| 445 |
('0.0100100', '1.001%'), |
| 446 |
('0.010100100', '1.01001%'), |
| 447 |
('0.000000', '0%'), |
| 448 |
('0', '0%'), |
| 449 |
('0.00', '0%'), |
| 450 |
('0.01', '1%'), |
| 451 |
('0.011', '1.1%'), |
| 452 |
('0.0110', '1.1%'), |
| 453 |
('0.0001', '0.01%'), |
| 454 |
('0.000100', '0.01%'), |
| 455 |
('0.0000100', '0.001%'), |
| 456 |
('0.00000100', '0.0001%'), |
| 457 |
]) |
| 458 |
def test_format_percent_precision(input_value, expected_value): |
| 459 |
# Test precision conservation. |
| 460 |
assert numbers.format_percent( |
| 461 |
decimal.Decimal(input_value), locale='en_US', decimal_quantization=False) == expected_value |
| 462 |
|
| 463 |
|
| 464 |
def test_format_percent_quantization(): |
| 465 |
# Test all locales. |
| 466 |
for locale_code in localedata.locale_identifiers(): |
| 467 |
assert numbers.format_percent( |
| 468 |
'0.9999999999', locale=locale_code, decimal_quantization=False).find('99999999') > -1 |
| 469 |
|
| 470 |
|
| 471 |
def test_format_scientific(): |
| 472 |
assert numbers.format_scientific(10000, locale='en_US') == '1E4' |
| 473 |
assert numbers.format_scientific(10000, locale='en_US', numbering_system="default") == '1E4' |
| 474 |
assert numbers.format_scientific(4234567, '#.#E0', locale='en_US') == '4.2E6' |
| 475 |
assert numbers.format_scientific(4234567, '0E0000', locale='en_US') == '4.234567E0006' |
| 476 |
assert numbers.format_scientific(4234567, '##0E00', locale='en_US') == '4.234567E06' |
| 477 |
assert numbers.format_scientific(4234567, '##00E00', locale='en_US') == '42.34567E05' |
| 478 |
assert numbers.format_scientific(4234567, '0,000E00', locale='en_US') == '4,234.567E03' |
| 479 |
assert numbers.format_scientific(4234567, '##0.#####E00', locale='en_US') == '4.23457E06' |
| 480 |
assert numbers.format_scientific(4234567, '##0.##E00', locale='en_US') == '4.23E06' |
| 481 |
assert numbers.format_scientific(42, '00000.000000E0000', locale='en_US') == '42000.000000E-0003' |
| 482 |
assert numbers.format_scientific(0.2, locale="ar_EG", numbering_system="default") == '2أس\u061c-1' |
| 483 |
|
| 484 |
|
| 485 |
def test_default_scientific_format(): |
| 486 |
""" Check the scientific format method auto-correct the rendering pattern |
| 487 |
in case of a missing fractional part. |
| 488 |
""" |
| 489 |
assert numbers.format_scientific(12345, locale='en_US') == '1.2345E4' |
| 490 |
assert numbers.format_scientific(12345.678, locale='en_US') == '1.2345678E4' |
| 491 |
assert numbers.format_scientific(12345, '#E0', locale='en_US') == '1.2345E4' |
| 492 |
assert numbers.format_scientific(12345.678, '#E0', locale='en_US') == '1.2345678E4' |
| 493 |
|
| 494 |
|
| 495 |
@pytest.mark.parametrize('input_value, expected_value', [ |
| 496 |
('10000', '1E4'), |
| 497 |
('1', '1E0'), |
| 498 |
('1.0', '1E0'), |
| 499 |
('1.1', '1.1E0'), |
| 500 |
('1.11', '1.11E0'), |
| 501 |
('1.110', '1.11E0'), |
| 502 |
('1.001', '1.001E0'), |
| 503 |
('1.00100', '1.001E0'), |
| 504 |
('01.00100', '1.001E0'), |
| 505 |
('101.00100', '1.01001E2'), |
| 506 |
('00000', '0E0'), |
| 507 |
('0', '0E0'), |
| 508 |
('0.0', '0E0'), |
| 509 |
('0.1', '1E-1'), |
| 510 |
('0.11', '1.1E-1'), |
| 511 |
('0.110', '1.1E-1'), |
| 512 |
('0.001', '1E-3'), |
| 513 |
('0.00100', '1E-3'), |
| 514 |
('00.00100', '1E-3'), |
| 515 |
('000.00100', '1E-3'), |
| 516 |
]) |
| 517 |
def test_format_scientific_precision(input_value, expected_value): |
| 518 |
# Test precision conservation. |
| 519 |
assert numbers.format_scientific( |
| 520 |
decimal.Decimal(input_value), locale='en_US', decimal_quantization=False) == expected_value |
| 521 |
|
| 522 |
|
| 523 |
def test_format_scientific_quantization(): |
| 524 |
# Test all locales. |
| 525 |
for locale_code in localedata.locale_identifiers(): |
| 526 |
assert numbers.format_scientific( |
| 527 |
'0.9999999999', locale=locale_code, decimal_quantization=False).find('999999999') > -1 |
| 528 |
|
| 529 |
|
| 530 |
def test_parse_number(): |
| 531 |
assert numbers.parse_number('1,099', locale='en_US') == 1099 |
| 532 |
assert numbers.parse_number('1.099', locale='de_DE') == 1099 |
| 533 |
assert numbers.parse_number('1٬099', locale='ar_EG', numbering_system="default") == 1099 |
| 534 |
|
| 535 |
with pytest.raises(numbers.NumberFormatError, match="'1.099,98' is not a valid number"): |
| 536 |
numbers.parse_number('1.099,98', locale='de') |
| 537 |
|
| 538 |
with pytest.raises(numbers.UnsupportedNumberingSystemError): |
| 539 |
numbers.parse_number('1.099,98', locale='en', numbering_system="unsupported") |
| 540 |
|
| 541 |
|
| 542 |
@pytest.mark.parametrize('string', [ |
| 543 |
'1 099', |
| 544 |
'1\xa0099', |
| 545 |
'1\u202f099', |
| 546 |
]) |
| 547 |
def test_parse_number_group_separator_can_be_any_space(string): |
| 548 |
assert numbers.parse_number(string, locale='fr') == 1099 |
| 549 |
|
| 550 |
|
| 551 |
def test_parse_decimal(): |
| 552 |
assert (numbers.parse_decimal('1,099.98', locale='en_US') |
| 553 |
== decimal.Decimal('1099.98')) |
| 554 |
assert numbers.parse_decimal('1.099,98', locale='de') == decimal.Decimal('1099.98') |
| 555 |
|
| 556 |
with pytest.raises(numbers.NumberFormatError, match="'2,109,998' is not a valid decimal number"): |
| 557 |
numbers.parse_decimal('2,109,998', locale='de') |
| 558 |
|
| 559 |
|
| 560 |
@pytest.mark.parametrize('string', [ |
| 561 |
'1 099,98', |
| 562 |
'1\xa0099,98', |
| 563 |
'1\u202f099,98', |
| 564 |
]) |
| 565 |
def test_parse_decimal_group_separator_can_be_any_space(string): |
| 566 |
assert decimal.Decimal('1099.98') == numbers.parse_decimal(string, locale='fr') |
| 567 |
|
| 568 |
|
| 569 |
def test_parse_grouping(): |
| 570 |
assert numbers.parse_grouping('##') == (1000, 1000) |
| 571 |
assert numbers.parse_grouping('#,###') == (3, 3) |
| 572 |
assert numbers.parse_grouping('#,####,###') == (3, 4) |
| 573 |
|
| 574 |
|
| 575 |
def test_parse_pattern(): |
| 576 |
|
| 577 |
# Original pattern is preserved |
| 578 |
np = numbers.parse_pattern('¤#,##0.00') |
| 579 |
assert np.pattern == '¤#,##0.00' |
| 580 |
|
| 581 |
np = numbers.parse_pattern('¤#,##0.00;(¤#,##0.00)') |
| 582 |
assert np.pattern == '¤#,##0.00;(¤#,##0.00)' |
| 583 |
|
| 584 |
# Given a NumberPattern object, we don't return a new instance. |
| 585 |
# However, we don't cache NumberPattern objects, so calling |
| 586 |
# parse_pattern with the same format string will create new |
| 587 |
# instances |
| 588 |
np1 = numbers.parse_pattern('¤ #,##0.00') |
| 589 |
np2 = numbers.parse_pattern('¤ #,##0.00') |
| 590 |
assert np1 is not np2 |
| 591 |
assert np1 is numbers.parse_pattern(np1) |
| 592 |
|
| 593 |
|
| 594 |
def test_parse_pattern_negative(): |
| 595 |
|
| 596 |
# No negative format specified |
| 597 |
np = numbers.parse_pattern('¤#,##0.00') |
| 598 |
assert np.prefix == ('¤', '-¤') |
| 599 |
assert np.suffix == ('', '') |
| 600 |
|
| 601 |
# Negative format is specified |
| 602 |
np = numbers.parse_pattern('¤#,##0.00;(¤#,##0.00)') |
| 603 |
assert np.prefix == ('¤', '(¤') |
| 604 |
assert np.suffix == ('', ')') |
| 605 |
|
| 606 |
# Negative sign is a suffix |
| 607 |
np = numbers.parse_pattern('¤ #,##0.00;¤ #,##0.00-') |
| 608 |
assert np.prefix == ('¤ ', '¤ ') |
| 609 |
assert np.suffix == ('', '-') |
| 610 |
|
| 611 |
|
| 612 |
def test_numberpattern_repr(): |
| 613 |
"""repr() outputs the pattern string""" |
| 614 |
|
| 615 |
# This implementation looks a bit funny, but that's cause strings are |
| 616 |
# repr'd differently in Python 2 vs 3 and this test runs under both. |
| 617 |
format = '¤#,##0.00;(¤#,##0.00)' |
| 618 |
np = numbers.parse_pattern(format) |
| 619 |
assert repr(format) in repr(np) |
| 620 |
|
| 621 |
|
| 622 |
def test_parse_static_pattern(): |
| 623 |
assert numbers.parse_pattern('Kun') # in the So locale in CLDR 30 |
| 624 |
# TODO: static patterns might not be correctly `apply()`ed at present |
| 625 |
|
| 626 |
|
| 627 |
def test_parse_decimal_nbsp_heuristics(): |
| 628 |
# Re https://github.com/python-babel/babel/issues/637 – |
| 629 |
# for locales (of which there are many) that use U+00A0 as the group |
| 630 |
# separator in numbers, it's reasonable to assume that input strings |
| 631 |
# with plain spaces actually should have U+00A0s instead. |
| 632 |
# This heuristic is only applied when strict=False. |
| 633 |
n = decimal.Decimal("12345.123") |
| 634 |
assert numbers.parse_decimal("12 345.123", locale="fi") == n |
| 635 |
assert numbers.parse_decimal(numbers.format_decimal(n, locale="fi"), locale="fi") == n |
| 636 |
|
| 637 |
|
| 638 |
def test_very_small_decimal_no_quantization(): |
| 639 |
assert numbers.format_decimal(decimal.Decimal('1E-7'), locale='en', decimal_quantization=False) == '0.0000001' |
| 640 |
|
| 641 |
|
| 642 |
def test_single_quotes_in_pattern(): |
| 643 |
assert numbers.format_decimal(123, "'@0.#'00'@01'", locale='en') == '@0.#120@01' |
| 644 |
|
| 645 |
assert numbers.format_decimal(123, "'$'''0", locale='en') == "$'123" |
| 646 |
|
| 647 |
assert numbers.format_decimal(12, "'#'0 o''clock", locale='en') == "#12 o'clock" |
| 648 |
|
| 649 |
|
| 650 |
def test_format_currency_with_none_locale_with_default(monkeypatch): |
| 651 |
"""Test that the default locale is used when locale is None.""" |
| 652 |
monkeypatch.setattr(numbers, "LC_MONETARY", "fi_FI") |
| 653 |
monkeypatch.setattr(numbers, "LC_NUMERIC", None) |
| 654 |
assert numbers.format_currency(0, "USD", locale=None) == "0,00\xa0$" |
| 655 |
|
| 656 |
|
| 657 |
def test_format_currency_with_none_locale(monkeypatch): |
| 658 |
"""Test that the API raises the "Empty locale identifier" error when locale is None, and the default is too.""" |
| 659 |
monkeypatch.setattr(numbers, "LC_MONETARY", None) # Pretend we couldn't find any locale when importing the module |
| 660 |
with pytest.raises(TypeError, match="Empty"): |
| 661 |
numbers.format_currency(0, "USD", locale=None) |
| 662 |
|
| 663 |
|
| 664 |
def test_format_decimal_with_none_locale_with_default(monkeypatch): |
| 665 |
"""Test that the default locale is used when locale is None.""" |
| 666 |
monkeypatch.setattr(numbers, "LC_NUMERIC", "fi_FI") |
| 667 |
monkeypatch.setattr(numbers, "LC_MONETARY", None) |
| 668 |
assert numbers.format_decimal("1.23", locale=None) == "1,23" |
| 669 |
|
| 670 |
|
| 671 |
def test_format_decimal_with_none_locale(monkeypatch): |
| 672 |
"""Test that the API raises the "Empty locale identifier" error when locale is None, and the default is too.""" |
| 673 |
monkeypatch.setattr(numbers, "LC_NUMERIC", None) # Pretend we couldn't find any locale when importing the module |
| 674 |
with pytest.raises(TypeError, match="Empty"): |
| 675 |
numbers.format_decimal(0, locale=None) |
| 676 |
|
| 677 |
|
| 678 |
def test_issue_1076_fixes(): |
| 679 |
# Japanese only defines short compact decimal formats; root aliases long to short. This used to raise a KeyError. |
| 680 |
assert numbers.format_compact_decimal(12345, format_type="long", locale="ja") == "1万" |
| 681 |
assert numbers.format_compact_decimal(2345678, format_type="long", locale="ja") == "235万" |
| 682 |
# Swahili does not define an accounting currency format; root aliases it to the standard format. |
| 683 |
assert numbers.format_currency(-2, "USD", format_type="accounting", locale="sw") == "-US$\xa02.00" |