Skip to content

Format NaN with the locale's symbol, without fraction digits or exponent - #1419

Closed
RavSinghChandan wants to merge 1 commit into
python-babel:masterfrom
RavSinghChandan:format-nan-symbol
Closed

RavSinghChandan wants to merge 1 commit into
python-babel:masterfrom
RavSinghChandan:format-nan-symbol

Conversation

@RavSinghChandan

Copy link
Copy Markdown

Formatting NaN treats it like a number with digits:

format_currency(Decimal('NaN'), 'USD', locale='en_US')      # '$NaN.00'
format_currency(Decimal('NaN'), 'EUR', locale='de_DE')      # 'NaN,00 €'
format_decimal(Decimal('NaN'), '#,##0.00', locale='en_US')  # 'NaN.00'
format_scientific(Decimal('NaN'), locale='en_US')           # 'NaNE0'
format_decimal(Decimal('NaN'), locale='fi_FI')              # 'NaN', but CLDR has 'epäluku'

Infinity is already replaced by the locale's symbol in _quantize_value; NaN now is too (number_symbols[...]['nan'], e.g. 'epäluku' in Finnish, 'ليس رقمًا' in Arabic), so no fraction digits are appended to it. Scientific notation is only rendered for finite numbers, which also turns format_scientific(Decimal('Infinity')) from '∞E0' into '∞'.

With the fix: '$NaN', 'NaN €', 'NaN', 'NaN', 'epäluku'. Finite numbers are unchanged. Assertions added to test_nan_and_infinity

@akx

akx commented Nov 2, 2026

Copy link
Copy Markdown
Member

How does this compare to #1416?

@RavSinghChandan

Copy link
Copy Markdown
Author

Same core fix, the NaN early return in _quantize_value. #1416 came first, sorry for not spotting it. This one adds two things on top:

it uses the locale's NaN symbol from CLDR, like infinity already does (epäluku in fi_FI), which changes plain format_decimal(nan) output for those locales
it skips the exponent for NaN and infinity in format_scientific (NaNE0 → NaN, ∞E0 → ∞)
If you'd rather keep this small, I'm happy to close this in favour of #1416, or to open just the format_scientific part separately on top of it.

@RavSinghChandan

Copy link
Copy Markdown
Author

Same core fix, the NaN early return in _quantize_value. #1416 came first, sorry for not spotting it. This one adds two things on top:

  • it uses the locale's NaN symbol from CLDR, like infinity already does (epäluku in fi_FI), which changes plain format_decimal(nan) output for those locales
  • it skips the exponent for NaN and infinity in format_scientific (NaNE0 → NaN, ∞E0 → ∞)

If you'd rather keep this small, I'm happy to close this in favour of #1416, or to open just the format_scientific part separately on top of it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants