Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Escape space after comma as decimal seperator #24079

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/matplotlib/tests/test_ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1352,6 +1352,17 @@ def test_engformatter_usetex_useMathText():
assert x_tick_label_text == ['$0$', '$500$', '$1$ k']


def test_locale_comma():
import locale
currentLocale = locale.getlocale()
locale.setlocale(locale.LC_ALL, 'deu_deu')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test will fail with "unsupported locale setting" if the locale is unavailable; at least this should be caught, and it may be better to just monkeypatch locale.format_string instead?

ticks = mticker.ScalarFormatter(useMathText=True, useLocale=True)
fmt = '$\\mathdefault{%1.1f}$'
formatted = ticks._format_maybe_minus_and_locale(fmt, 0.5)
locale.setlocale(locale.LC_ALL, currentLocale)
assert formatted == '$\\mathdefault{0{,}5}$'


class TestPercentFormatter:
percent_data = [
# Check explicitly set decimals over different intervals and values
Expand Down
7 changes: 5 additions & 2 deletions lib/matplotlib/ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,8 +532,11 @@ def _format_maybe_minus_and_locale(self, fmt, arg):
"""
Format *arg* with *fmt*, applying Unicode minus and locale if desired.
"""
return self.fix_minus(locale.format_string(fmt, (arg,), True)
if self._useLocale else fmt % arg)
formatted = self.fix_minus(locale.format_string(fmt, (arg,), True)
if self._useLocale else fmt % arg)
if (self.get_useMathText()): # removed unintended space after comma
formatted = formatted.replace(',', '{,}')
Copy link
Contributor

@anntzer anntzer Oct 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would incorrectly also escape commas actually present in fmt.

I guess a simple-ish solution could be to str.split fmt over commas, apply locale.format_string over each part, escape resulting commas over each formatted part, and join again using unescaped commas.

Edit: basically

        return self.fix_minus(
            # Escape commas introduced by format_string but not those present
            # from the beginning in fmt.
            ",".join(locale.format_string(part, (arg,), True)
                     .replace(",", "{,}")
                     for part in fmt.split(","))
            if self._useLocale else
            fmt % arg)

return formatted

def get_useMathText(self):
"""
Expand Down