@@ -608,6 +608,13 @@ def set_useLocale(self, val):
608
608
609
609
useLocale = property (fget = get_useLocale , fset = set_useLocale )
610
610
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
+
611
618
def get_useMathText (self ):
612
619
"""
613
620
Return whether to use fancy math formatting.
@@ -646,11 +653,7 @@ def __call__(self, x, pos=None):
646
653
xp = (x - self .offset ) / (10. ** self .orderOfMagnitude )
647
654
if abs (xp ) < 1e-8 :
648
655
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 )
654
657
655
658
def set_scientific (self , b ):
656
659
"""
@@ -730,19 +733,23 @@ def format_data_short(self, value):
730
733
(math .floor (math .log10 (abs (value ))) + 1 if value else 1 )
731
734
- math .floor (math .log10 (delta )))
732
735
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 )
737
737
738
738
def format_data (self , value ):
739
739
# docstring inherited
740
- if self ._useLocale :
741
- s = locale .format_string ('%1.10e' , (value ,))
740
+ e = math .floor (math .log10 (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.10e" , s )
745
+ if e == 0 :
746
+ return significand
747
+ elif self ._useMathText or self ._usetex :
748
+ exponent = "10^{%d}" % exponent
749
+ return (exponent if s == 1 # reformat 1x10^y as 10^y
750
+ else rf"{ significand } \times { exponent } " )
742
751
else :
743
- s = '%1.10e' % value
744
- s = self ._formatSciNotation (s )
745
- return self .fix_minus (s )
752
+ return f"{ significand } e{ exponent } "
746
753
747
754
def get_offset (self ):
748
755
"""
@@ -890,35 +897,6 @@ def _set_format(self):
890
897
if self ._usetex or self ._useMathText :
891
898
self .format = r'$\mathdefault{%s}$' % self .format
892
899
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
-
922
900
923
901
class LogFormatter (Formatter ):
924
902
"""
0 commit comments