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 (#46473)
* 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.
* Update parsing-numeric.md
* Update standard-numeric-format-strings.md
* Update parsing-numeric.md
* Update standard-numeric-format-strings.md
---------
Co-authored-by: Andy (Steve) De George <[email protected]>
Copy file name to clipboardExpand all lines: docs/standard/base-types/parsing-numeric.md
+6Lines changed: 6 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -61,6 +61,7 @@ All numeric types have two static parsing methods, `Parse` and `TryParse`, that
61
61
|<xref:System.Globalization.NumberStyles.AllowThousands?displayProperty=nameWithType>|The group separator is permitted. The group separator character is determined by the <xref:System.Globalization.NumberFormatInfo.NumberGroupSeparator%2A?displayProperty=nameWithType> or <xref:System.Globalization.NumberFormatInfo.CurrencyGroupSeparator%2A?displayProperty=nameWithType> property.|
62
62
|<xref:System.Globalization.NumberStyles.AllowCurrencySymbol?displayProperty=nameWithType>|The currency symbol is permitted. The currency symbol is defined by the <xref:System.Globalization.NumberFormatInfo.CurrencySymbol%2A?displayProperty=nameWithType> property.|
63
63
|<xref:System.Globalization.NumberStyles.AllowHexSpecifier?displayProperty=nameWithType>|The string to be parsed is interpreted as a hexadecimal number. It can include the hexadecimal digits 0-9, A-F, and a-f. This flag can be used only to parse integer values.|
64
+
|<xref:System.Globalization.NumberStyles.AllowBinarySpecifier?displayProperty=nameWithType>|The string to be parsed is interpreted as a binary number. It can include the binary digits 0 and 1. This flag can be used only to parse integer values.|
64
65
65
66
In addition, the <xref:System.Globalization.NumberStyles> enumeration provides the following composite styles, which include multiple <xref:System.Globalization.NumberStyles> flags.
66
67
@@ -72,6 +73,11 @@ All numeric types have two static parsing methods, `Parse` and `TryParse`, that
72
73
|<xref:System.Globalization.NumberStyles.Currency?displayProperty=nameWithType>|Includes all styles except <xref:System.Globalization.NumberStyles.AllowExponent?displayProperty=nameWithType> and <xref:System.Globalization.NumberStyles.AllowHexSpecifier?displayProperty=nameWithType>.|
73
74
|<xref:System.Globalization.NumberStyles.Any?displayProperty=nameWithType>|Includes all styles except <xref:System.Globalization.NumberStyles.AllowHexSpecifier?displayProperty=nameWithType>.|
74
75
|<xref:System.Globalization.NumberStyles.HexNumber?displayProperty=nameWithType>|Includes the <xref:System.Globalization.NumberStyles.AllowLeadingWhite?displayProperty=nameWithType>, <xref:System.Globalization.NumberStyles.AllowTrailingWhite?displayProperty=nameWithType>, and <xref:System.Globalization.NumberStyles.AllowHexSpecifier?displayProperty=nameWithType> styles.|
76
+
|<xref:System.Globalization.NumberStyles.BinaryNumber?displayProperty=nameWithType>|Includes the <xref:System.Globalization.NumberStyles.AllowLeadingWhite?displayProperty=nameWithType>, <xref:System.Globalization.NumberStyles.AllowTrailingWhite?displayProperty=nameWithType>, and <xref:System.Globalization.NumberStyles.AllowBinarySpecifier?displayProperty=nameWithType> styles.|
77
+
78
+
## Parsing binary and hexadecimal BigIntegers
79
+
80
+
When parsing <xref:System.Numerics.BigInteger> with the <xref:System.Globalization.NumberStyles.AllowHexSpecifier> or <xref:System.Globalization.NumberStyles.AllowBinarySpecifier> flags, the input string is interpreted as a hexadecimal/binary number of exactly the length the string has. For instance, parsing `"11"` as a binary BigInteger yields `-1`, because that is the interpretation of `11` as a signed two's complement value with exactly 2 digits. If you want a positive result, add a leading `0`, such as `"011"` which is parsed as `3`.
Copy file name to clipboardExpand all lines: docs/standard/base-types/standard-numeric-format-strings.md
+4Lines changed: 4 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -98,6 +98,8 @@ 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. For instance, the number `3` converted with the format specifier `"B2"` is `"011"`. That's because the binary number `"11"` represents the negative value `-1`, as it is interpreted as a number with exactly `2` bits due to the `"B2"` format.
102
+
101
103
The result string is not affected by the formatting information of the current <xref:System.Globalization.NumberFormatInfo> object.
102
104
103
105
<aname="CFormatString"></a>
@@ -309,6 +311,8 @@ The hexadecimal ("X") format specifier converts a number to a string of hexadeci
309
311
310
312
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
313
314
+
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. For example, the number `F` converted with the format specifier `"X1"` is `0F` because the hexadecimal number `F` represents the negative value `-1`.
315
+
312
316
The result string is not affected by the formatting information of the current <xref:System.Globalization.NumberFormatInfo> object.
313
317
314
318
The following example formats <xref:System.Int32> values with the hexadecimal format specifier.
0 commit comments