@@ -758,8 +758,7 @@ public static BigInteger ToBigInteger(BigInteger self) {
758758
759759 if ( spec . Fill == '0' && spec . Width > 1 ) {
760760 digits = FormattingHelper . ToCultureString ( val , culture . NumberFormat , spec , ( spec . Sign != null && spec . Sign != '-' || self < 0 ) ? spec . Width - 1 : null ) ;
761- }
762- else {
761+ } else {
763762 digits = FormattingHelper . ToCultureString ( val , culture . NumberFormat , spec ) ;
764763 }
765764 break ;
@@ -790,24 +789,36 @@ public static BigInteger ToBigInteger(BigInteger self) {
790789 digits = DoubleOps . DoubleToFormatString ( context , ToDouble ( val ) , spec ) ;
791790 break ;
792791 case 'X' :
793- digits = AbsToHex ( val , lowercase : false ) ;
792+ digits = ToHexDigits ( val , lowercase : false ) ;
793+ if ( spec . ThousandsUnderscore ) {
794+ digits = FormattingHelper . AddUnderscores ( digits , spec , self . IsNegative ( ) ) ;
795+ }
794796 break ;
795797 case 'x' :
796- digits = AbsToHex ( val , lowercase : true ) ;
798+ digits = ToHexDigits ( val , lowercase : true ) ;
799+ if ( spec . ThousandsUnderscore ) {
800+ digits = FormattingHelper . AddUnderscores ( digits , spec , self . IsNegative ( ) ) ;
801+ }
797802 break ;
798803 case 'o' : // octal
799- digits = ToOctal ( val , lowercase : true ) ;
804+ digits = ToOctalDigits ( val ) ;
805+ if ( spec . ThousandsUnderscore ) {
806+ digits = FormattingHelper . AddUnderscores ( digits , spec , self . IsNegative ( ) ) ;
807+ }
800808 break ;
801809 case 'b' : // binary
802- digits = ToBinary ( val , includeType : false , lowercase : true ) ;
810+ digits = ToBinaryDigits ( val ) ;
811+ if ( spec . ThousandsUnderscore ) {
812+ digits = FormattingHelper . AddUnderscores ( digits , spec , self . IsNegative ( ) ) ;
813+ }
803814 break ;
804815 case 'c' : // single char
805816 int iVal ;
806817 if ( spec . Sign != null ) {
807818 throw PythonOps . ValueError ( "Sign not allowed with integer format specifier 'c'" ) ;
808819 } else if ( ! self . AsInt32 ( out iVal ) ) {
809820 throw PythonOps . OverflowError ( "Python int too large to convert to System.Int32" ) ;
810- } else if ( iVal < 0 || iVal > 0x10ffff ) {
821+ } else if ( iVal < 0 || iVal > 0x10ffff ) {
811822 throw PythonOps . OverflowError ( "%c arg not in range(0x110000)" ) ;
812823 }
813824
@@ -1025,8 +1036,10 @@ public static TypeCode GetTypeCode(BigInteger self) {
10251036
10261037 #region Helpers
10271038
1039+ /// <summary>
1040+ /// Unlike ConvertToDouble, this method produces a Python-specific overflow error messge.
1041+ /// </summary>
10281042 internal static double ToDouble ( BigInteger self ) {
1029- // Unlike ConvertToDouble, this method produces a Python-specific overflow error messge.
10301043 if ( MathUtils . TryToFloat64 ( self , out double res ) ) {
10311044 return res ;
10321045 }
@@ -1037,27 +1050,24 @@ internal static string AbsToHex(BigInteger val, bool lowercase) {
10371050 return ToDigits ( val , 16 , lowercase ) ;
10381051 }
10391052
1040- private static string ToOctal ( BigInteger val , bool lowercase ) {
1041- return ToDigits ( val , 8 , lowercase ) ;
1053+ private static string ToHexDigits ( BigInteger val , bool lowercase ) {
1054+ Debug . Assert ( val >= 0 ) ;
1055+ return ToDigits ( val , 16 , lower : lowercase ) ;
10421056 }
10431057
1044- internal static string ToBinary ( BigInteger val ) {
1045- string res = ToBinary ( BigInteger . Abs ( val ) , true , true ) ;
1046- if ( val . IsNegative ( ) ) {
1047- res = "-" + res ;
1048- }
1049- return res ;
1058+ private static string ToOctalDigits ( BigInteger val ) {
1059+ Debug . Assert ( val >= 0 ) ;
1060+ return ToDigits ( val , 8 , lower : false ) ;
10501061 }
10511062
1052- private static string ToBinary ( BigInteger val , bool includeType , bool lowercase ) {
1053- Debug . Assert ( ! val . IsNegative ( ) ) ;
1054-
1055- string digits = ToDigits ( val , 2 , lowercase ) ;
1063+ private static string ToBinaryDigits ( BigInteger val ) {
1064+ Debug . Assert ( val >= 0 ) ;
1065+ return ToDigits ( val , 2 , lower : false ) ;
1066+ }
10561067
1057- if ( includeType ) {
1058- digits = ( lowercase ? "0b" : "0B" ) + digits ;
1059- }
1060- return digits ;
1068+ internal static string ToBinary ( BigInteger val ) {
1069+ var digits = ToBinaryDigits ( BigInteger . Abs ( val ) ) ;
1070+ return ( ( val < 0 ) ? "-0b" : "0b" ) + digits ;
10611071 }
10621072
10631073 private static string /*!*/ ToDigits ( BigInteger /*!*/ val , int radix , bool lower ) {
@@ -1066,12 +1076,12 @@ private static string ToBinary(BigInteger val, bool includeType, bool lowercase)
10661076 }
10671077
10681078 StringBuilder str = new StringBuilder ( ) ;
1079+ char a = lower ? 'a' : 'A' ;
10691080
10701081 while ( val != 0 ) {
10711082 int digit = ( int ) ( val % radix ) ;
10721083 if ( digit < 10 ) str . Append ( ( char ) ( ( digit ) + '0' ) ) ;
1073- else if ( lower ) str . Append ( ( char ) ( ( digit - 10 ) + 'a' ) ) ;
1074- else str . Append ( ( char ) ( ( digit - 10 ) + 'A' ) ) ;
1084+ else str . Append ( ( char ) ( ( digit - 10 ) + a ) ) ;
10751085
10761086 val /= radix ;
10771087 }
0 commit comments