1

I'm trying to print a number in a scientific number format (with exponent) using the package babel and a German locale:

>>> from babel.numbers import format_scientific
>>> x = 123456.789
>>> format_scientific(x, format="0.000E0", locale="de_DE")
'1.235E5'

I would have expected an output of '1,235E5' instead (note the comma instead of the point as the decimal seperator). Maybe format_scientific isn't supposed to be called directly, and I have more luck with format_decimal? Nope:

>>> from babel.numbers import format_decimal
>>> format_decimal(x, format="0.000E0", locale="de_DE")
'1.235E5'

I've tested it with Python 3.6 and Babel 2.3.4. This isn't the latest version of Babel, but their changelog doesn't indicate that they've fixed a big, glaring error in their handling of the scientific format, nor did I find anything on their bugtracker. Since it seems unlikely that such an error would go unnoticed, I'm obviously doing something wrong or operate under some faulty assumption.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Jan
  • 892
  • 6
  • 7
  • With the included `E` you're now entering the scientific / engineering / computing domain where universal standards overrule local standards. I'm guessing that's what is happening here. The `.` is also unambiguous since scientific format will never have both `,` and `.`. – FHTMitchell May 16 '18 at 08:49
  • Babel claims to follow the rules from http://unicode.org/reports/tr35/tr35-numbers.html#Number_Format_Patterns. I don't see any mentioning there that the mantissa is *not* localized; quite to the contrary, it explicitely mentions using a localized plus sign, which would indicate to me that localization still happens. – Jan May 16 '18 at 09:02
  • Although, interestingly, the format ``"0,000.0E0"`` produces ``'1234.6E2'`` and doesn't include the requested thousands separator. – Jan May 16 '18 at 09:10
  • Hmm well that makes sense. In your second example though I guess that is just undefined behaviour since `1,234.6E2` is not scientific format. – FHTMitchell May 16 '18 at 09:11
  • Some more experiments: there are several locales which produce a different result. ``"lt_LT"`` for example produces ``'1.235×10^05'``, and ``"uz_Arab"`` produces ``'1.235×۱۰^05'``. But they all use a dot as the decimal seperator. – Jan May 16 '18 at 09:26
  • 1
    I think this is something you should take directly to the developers https://github.com/python-babel/babel. It seems either intended or undocumented or a bug. – FHTMitchell May 16 '18 at 09:27
  • Will do, if this question doesn't elicite more insight. Thanks for the comments. – Jan May 16 '18 at 09:31

0 Answers0