You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add note on BigInteger bin/hex formatting of positive values
See dotnet/runtime#115618.
It's somewhat unexpected that "print `3` with 2 binary digits" returns a string of length 3 (`"011"`), but also makes sense given the round-trip requirement and the historical context of not using `-` for negative bin/hex numbers.
I think documenting this behavior would be useful.
Copy file name to clipboardExpand all lines: docs/standard/base-types/standard-numeric-format-strings.md
+6Lines changed: 6 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -98,6 +98,9 @@ The binary ("B") format specifier converts a number to a string of binary digits
98
98
99
99
The precision specifier indicates the minimum number of digits desired in the resulting string. If required, the number is padded with zeros to its left to produce the number of digits given by the precision specifier.
100
100
101
+
For <xref:System.Numerics.BigInteger>, positive values always have a leading zero to distinguish them from negative values. This ensures the output round-trips to the original value when parsed.
102
+
For instance, the number `3` converted with the format specifier `"B2"` is `011` because the binary number `11` represents the negative value `-1`.
103
+
101
104
The result string is not affected by the formatting information of the current <xref:System.Globalization.NumberFormatInfo> object.
102
105
103
106
<aname="CFormatString"></a>
@@ -309,6 +312,9 @@ The hexadecimal ("X") format specifier converts a number to a string of hexadeci
309
312
310
313
The precision specifier indicates the minimum number of digits desired in the resulting string. If required, the number is padded with zeros to its left to produce the number of digits given by the precision specifier.
311
314
315
+
For <xref:System.Numerics.BigInteger>, positive values always have a leading zero to distinguish them from negative values. This ensures the output round-trips to the original value when parsed.
316
+
For instance, the number `F` converted with the format specifier `"X1"` is `0F` because the hexadecimal number `F` represents the negative value `-1`.
317
+
312
318
The result string is not affected by the formatting information of the current <xref:System.Globalization.NumberFormatInfo> object.
313
319
314
320
The following example formats <xref:System.Int32> values with the hexadecimal format specifier.
0 commit comments