@@ -742,10 +742,8 @@ public static BigInteger ToBigInteger(BigInteger self) {
742742 throw PythonOps . ValueError ( "Precision not allowed in integer format specifier" ) ;
743743 }
744744
745- BigInteger val = self ;
746- if ( self < 0 ) {
747- val = - self ;
748- }
745+ BigInteger val = self . Abs ( ) ;
746+
749747 string digits ;
750748
751749 switch ( spec . Type ) {
@@ -758,84 +756,50 @@ public static BigInteger ToBigInteger(BigInteger self) {
758756 goto case 'd' ;
759757 }
760758
761- digits = FormattingHelper . ToCultureString ( val , context . LanguageContext . NumericCulture . NumberFormat , spec ) ;
759+ if ( spec . Fill == '0' && spec . Width > 1 ) {
760+ digits = FormattingHelper . ToCultureString ( val , culture . NumberFormat , spec , ( spec . Sign != null && spec . Sign != '-' || self < 0 ) ? spec . Width - 1 : null ) ;
761+ }
762+ else {
763+ digits = FormattingHelper . ToCultureString ( val , culture . NumberFormat , spec ) ;
764+ }
762765 break ;
763766 case null :
764767 case 'd' :
765- if ( spec . ThousandsComma ) {
766- var width = spec . Width ?? 0 ;
768+ if ( spec . ThousandsComma || spec . ThousandsUnderscore ) {
769+ var numberFormat = spec . ThousandsUnderscore ? FormattingHelper . InvariantUnderscoreNumberInfo : CultureInfo . InvariantCulture . NumberFormat ;
770+
767771 // If we're inserting commas, and we're padding with leading zeros.
768772 // AlignNumericText won't know where to place the commas,
769- // so force .Net to help us out here.
770- if ( spec . Fill . HasValue && spec . Fill . Value == '0' && width > 1 ) {
771- digits = val . ToString ( FormattingHelper . ToCultureString ( self , FormattingHelper . InvariantCommaNumberInfo , spec ) ) ;
773+ // so use FormattingHelper.ToCultureString for that support.
774+ if ( spec . Fill == '0' && spec . Width > 1 ) {
775+ digits = FormattingHelper . ToCultureString ( val , numberFormat , spec , ( spec . Sign != null && spec . Sign != '-' || self < 0 ) ? spec . Width - 1 : null ) ;
776+ } else {
777+ digits = val . ToString ( "#,0" , numberFormat ) ;
772778 }
773- else {
774- digits = val . ToString ( "#,0" , CultureInfo . InvariantCulture ) ;
775- }
776- }
777- else {
779+ } else {
778780 digits = val . ToString ( "D" , CultureInfo . InvariantCulture ) ;
779781 }
780782 break ;
781783 case '%' :
782- if ( spec . ThousandsComma ) {
783- digits = val . ToString ( "#,0.000000%" , CultureInfo . InvariantCulture ) ;
784- } else {
785- digits = val . ToString ( "0.000000%" , CultureInfo . InvariantCulture ) ;
786- }
787- break ;
788784 case 'e' :
789- if ( spec . ThousandsComma ) {
790- digits = val . ToString ( "#,0.000000e+00" , CultureInfo . InvariantCulture ) ;
791- } else {
792- digits = val . ToString ( "0.000000e+00" , CultureInfo . InvariantCulture ) ;
793- }
794- break ;
795785 case 'E' :
796- if ( spec . ThousandsComma ) {
797- digits = val . ToString ( "#,0.000000E+00" , CultureInfo . InvariantCulture ) ;
798- } else {
799- digits = val . ToString ( "0.000000E+00" , CultureInfo . InvariantCulture ) ;
800- }
801- break ;
802786 case 'f' :
803787 case 'F' :
804- if ( spec . ThousandsComma ) {
805- digits = val . ToString ( "#,########0.000000" , CultureInfo . InvariantCulture ) ;
806- } else {
807- digits = val . ToString ( "#########0.000000" , CultureInfo . InvariantCulture ) ;
808- }
809- break ;
810788 case 'g' :
811- if ( val >= 1000000 ) {
812- digits = val . ToString ( "0.#####e+00" , CultureInfo . InvariantCulture ) ;
813- } else if ( spec . ThousandsComma ) {
814- goto case 'd' ;
815- } else {
816- digits = val . ToString ( CultureInfo . InvariantCulture ) ;
817- }
818- break ;
819789 case 'G' :
820- if ( val >= 1000000 ) {
821- digits = val . ToString ( "0.#####E+00" , CultureInfo . InvariantCulture ) ;
822- } else if ( spec . ThousandsComma ) {
823- goto case 'd' ;
824- } else {
825- digits = val . ToString ( CultureInfo . InvariantCulture ) ;
826- }
790+ digits = DoubleOps . DoubleToFormatString ( context , ToDouble ( val ) , spec ) ;
827791 break ;
828792 case 'X' :
829- digits = AbsToHex ( val , false ) ;
793+ digits = AbsToHex ( val , lowercase : false ) ;
830794 break ;
831795 case 'x' :
832- digits = AbsToHex ( val , true ) ;
796+ digits = AbsToHex ( val , lowercase : true ) ;
833797 break ;
834798 case 'o' : // octal
835- digits = ToOctal ( val , true ) ;
799+ digits = ToOctal ( val , lowercase : true ) ;
836800 break ;
837801 case 'b' : // binary
838- digits = ToBinary ( val , false , true ) ;
802+ digits = ToBinary ( val , includeType : false , lowercase : true ) ;
839803 break ;
840804 case 'c' : // single char
841805 int iVal ;
0 commit comments