@@ -608,6 +608,13 @@ def set_useLocale(self, val):
608608
609609 useLocale = property (fget = get_useLocale , fset = set_useLocale )
610610
611+ def _format_maybe_minus_and_locale (self , fmt , arg ):
612+ """
613+ Format *arg* with *fmt*, applying unicode minus and locale if desired.
614+ """
615+ return self .fix_minus (locale .format_string (fmt , (arg ,))
616+ if self ._useLocale else fmt % arg )
617+
611618 def get_useMathText (self ):
612619 """
613620 Return whether to use fancy math formatting.
@@ -646,11 +653,7 @@ def __call__(self, x, pos=None):
646653 xp = (x - self .offset ) / (10. ** self .orderOfMagnitude )
647654 if abs (xp ) < 1e-8 :
648655 xp = 0
649- if self ._useLocale :
650- s = locale .format_string (self .format , (xp ,))
651- else :
652- s = self .format % xp
653- return self .fix_minus (s )
656+ return self ._format_maybe_minus_and_locale (self .format , xp )
654657
655658 def set_scientific (self , b ):
656659 """
@@ -730,19 +733,23 @@ def format_data_short(self, value):
730733 (math .floor (math .log10 (abs (value ))) + 1 if value else 1 )
731734 - math .floor (math .log10 (delta )))
732735 fmt = f"%-#.{ sig_digits } g"
733- return (
734- self .fix_minus (
735- locale .format_string (fmt , (value ,)) if self ._useLocale else
736- fmt % value ))
736+ return self ._format_maybe_minus_and_locale (fmt , value )
737737
738738 def format_data (self , value ):
739739 # docstring inherited
740- if self ._useLocale :
741- s = locale .format_string ('%1.10e' , (value ,))
740+ e = math .floor (math .log10 (abs (value )))
741+ s = round (value / 10 ** e , 10 )
742+ exponent = self ._format_maybe_minus_and_locale ("%d" , e )
743+ significand = self ._format_maybe_minus_and_locale (
744+ "%d" if s % 1 == 0 else "%1.10f" , s )
745+ if e == 0 :
746+ return significand
747+ elif self ._useMathText or self ._usetex :
748+ exponent = "10^{%s}" % exponent
749+ return (exponent if s == 1 # reformat 1x10^y as 10^y
750+ else rf"{ significand } \times { exponent } " )
742751 else :
743- s = '%1.10e' % value
744- s = self ._formatSciNotation (s )
745- return self .fix_minus (s )
752+ return f"{ significand } e{ exponent } "
746753
747754 def get_offset (self ):
748755 """
@@ -890,35 +897,6 @@ def _set_format(self):
890897 if self ._usetex or self ._useMathText :
891898 self .format = r'$\mathdefault{%s}$' % self .format
892899
893- def _formatSciNotation (self , s ):
894- # transform 1e+004 into 1e4, for example
895- if self ._useLocale :
896- decimal_point = locale .localeconv ()['decimal_point' ]
897- positive_sign = locale .localeconv ()['positive_sign' ]
898- else :
899- decimal_point = '.'
900- positive_sign = '+'
901- tup = s .split ('e' )
902- try :
903- significand = tup [0 ].rstrip ('0' ).rstrip (decimal_point )
904- sign = tup [1 ][0 ].replace (positive_sign , '' )
905- exponent = tup [1 ][1 :].lstrip ('0' )
906- if self ._useMathText or self ._usetex :
907- if significand == '1' and exponent != '' :
908- # reformat 1x10^y as 10^y
909- significand = ''
910- if exponent :
911- exponent = '10^{%s%s}' % (sign , exponent )
912- if significand and exponent :
913- return r'%s{\times}%s' % (significand , exponent )
914- else :
915- return r'%s%s' % (significand , exponent )
916- else :
917- s = ('%se%s%s' % (significand , sign , exponent )).rstrip ('e' )
918- return s
919- except IndexError :
920- return s
921-
922900
923901class LogFormatter (Formatter ):
924902 """
0 commit comments