|
115 | 115 | - By calling the <xref:System.Globalization.NumberFormatInfo.GetInstance%2A> method with a <xref:System.Globalization.CultureInfo> object that represents the current culture.
|
116 | 116 |
|
117 | 117 | The following example uses these three ways to create <xref:System.Globalization.NumberFormatInfo> objects that represent the formatting conventions of the current culture. It also retrieves the value of the <xref:System.Globalization.NumberFormatInfo.IsReadOnly%2A> property to illustrate that each object is read-only.
|
118 |
| - |
119 |
| - [!code-csharp[System.Globalization.NumberFormatInfo.Class.Instantiate#1](~/samples/snippets/csharp/VS_Snippets_CLR_System/system.globalization.numberformatinfo.class.instantiate/cs/instantiate1.cs#1)] |
120 |
| - [!code-vb[System.Globalization.NumberFormatInfo.Class.Instantiate#1](~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.globalization.numberformatinfo.class.instantiate/vb/instantiate1.vb#1)] |
121 |
| - |
| 118 | + |
| 119 | + :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.globalization.numberformatinfo.class.instantiate/cs/instantiate1.cs" interactive="try-dotnet" id="Snippet1"::: |
| 120 | + :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.globalization.numberformatinfo.class.instantiate/vb/instantiate1.vb" id="Snippet1"::: |
| 121 | +
|
122 | 122 | You can create a writable <xref:System.Globalization.NumberFormatInfo> object that represents the conventions of the current thread culture in any of the following ways:
|
123 | 123 |
|
124 | 124 | - By retrieving a <xref:System.Globalization.NumberFormatInfo> object in any of the ways illustrated in the previous code example, and calling the <xref:System.Globalization.NumberFormatInfo.Clone%2A> method on the returned <xref:System.Globalization.NumberFormatInfo> object. This creates a copy of the original <xref:System.Globalization.NumberFormatInfo> object, except that its <xref:System.Globalization.NumberFormatInfo.IsReadOnly%2A> property is `false`.
|
|
150 | 150 | - By calling the parameterless <xref:System.Globalization.NumberFormatInfo.%23ctor%2A> class constructor. The returned <xref:System.Globalization.NumberFormatInfo> object is read/write.
|
151 | 151 |
|
152 | 152 | The following example uses each of these methods to instantiate a <xref:System.Globalization.NumberFormatInfo> object that represents the invariant culture. It then indicates whether the object is read-only,
|
153 |
| - |
154 |
| - [!code-csharp[System.Globalization.NumberFormatInfo.Class.Instantiate#4](~/samples/snippets/csharp/VS_Snippets_CLR_System/system.globalization.numberformatinfo.class.instantiate/cs/instantiate4.cs#4)] |
155 |
| - [!code-vb[System.Globalization.NumberFormatInfo.Class.Instantiate#4](~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.globalization.numberformatinfo.class.instantiate/vb/instantiate4.vb#4)] |
156 |
| - |
| 153 | + |
| 154 | + :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.globalization.numberformatinfo.class.instantiate/cs/instantiate4.cs" interactive="try-dotnet" id="Snippet4"::: |
| 155 | + :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.globalization.numberformatinfo.class.instantiate/vb/instantiate4.vb" id="Snippet4"::: |
| 156 | +
|
157 | 157 | <a name="Instantiating_Specific"></a>
|
158 | 158 | ## Instantiating a NumberFormatInfo object for a specific culture
|
159 | 159 | A specific culture represents a language that is spoken in a particular country/region. For example, en-US is a specific culture that represents the English language spoken in the United States, and en-CA is a specific culture that represents the English language spoken in Canada. You can instantiate a <xref:System.Globalization.NumberFormatInfo> object that represents the formatting conventions of a specific culture in the following ways:
|
|
167 | 167 | - By calling one of the <xref:System.Globalization.CultureInfo.%23ctor%2A?displayProperty=nameWithType> class constructors and retrieving the value of the returned <xref:System.Globalization.CultureInfo> object's <xref:System.Globalization.CultureInfo.NumberFormat%2A> property. The returned <xref:System.Globalization.NumberFormatInfo> object is read/write.
|
168 | 168 |
|
169 | 169 | The following example uses these four ways to create a <xref:System.Globalization.NumberFormatInfo> object that reflects the formatting conventions of the Indonesian (Indonesia) culture. It also indicates whether each object is read-only.
|
170 |
| - |
171 |
| - [!code-csharp[System.Globalization.NumberFormatInfo.Class.Instantiate#5](~/samples/snippets/csharp/VS_Snippets_CLR_System/system.globalization.numberformatinfo.class.instantiate/cs/instantiate5.cs#5)] |
172 |
| - [!code-vb[System.Globalization.NumberFormatInfo.Class.Instantiate#5](~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.globalization.numberformatinfo.class.instantiate/vb/instantiate5.vb#5)] |
173 |
| - |
| 170 | + |
| 171 | + :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.globalization.numberformatinfo.class.instantiate/cs/instantiate5.cs" interactive="try-dotnet" id="Snippet5"::: |
| 172 | + :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.globalization.numberformatinfo.class.instantiate/vb/instantiate5.vb" id="Snippet5"::: |
| 173 | +
|
174 | 174 | <a name="Instantiating_Neutral"></a>
|
175 | 175 | ## Instantiating a NumberFormatInfo object for a neutral culture
|
176 | 176 | A neutral culture represents a culture or language that is independent of a country/region. It is typically the parent of one or more specific cultures. For example, fr is a neutral culture for the French language and the parent of the fr-FR culture. You create a <xref:System.Globalization.NumberFormatInfo> object that represents the formatting conventions of a neutral culture in the same way that you create a <xref:System.Globalization.NumberFormatInfo> object that represents the formatting conventions of a specific culture.
|
|
240 | 240 | <a name="properties"></a>
|
241 | 241 | ## Format strings and NumberFormatInfo properties
|
242 | 242 | Every formatting operation uses either a standard or a custom numeric format string to produce a result string from a number. In some cases, the use of a format string to produce a result string is explicit, as in the following example. This code calls the <xref:System.Decimal.ToString%28System.IFormatProvider%29?displayProperty=nameWithType> method to convert a <xref:System.Decimal> value to a number of different string representations by using the formatting conventions of the en-US culture.
|
243 |
| - |
244 |
| - [!code-csharp[System.Globalization.NumberFormatInfo.Class#2](~/samples/snippets/csharp/VS_Snippets_CLR_System/system.globalization.numberformatinfo.class/cs/properties1.cs#2)] |
245 |
| - [!code-vb[System.Globalization.NumberFormatInfo.Class#2](~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.globalization.numberformatinfo.class/vb/properties1.vb#2)] |
246 |
| - |
| 243 | + |
| 244 | + :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.globalization.numberformatinfo.class/cs/properties1.cs" interactive="try-dotnet" id="Snippet2"::: |
| 245 | + :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.globalization.numberformatinfo.class/vb/properties1.vb" id="Snippet2"::: |
| 246 | +
|
247 | 247 | In other cases, the use of a format string is implicit. For example, in the following method calls to the default or parameterless <xref:System.Decimal.ToString?displayProperty=nameWithType> method, the value of the <xref:System.Decimal> instance is formatted by using the general ("G") format specifier and the conventions of the current culture, which in this case is the en-US culture.
|
248 |
| - |
249 |
| - [!code-csharp[System.Globalization.NumberFormatInfo.Class#3](~/samples/snippets/csharp/VS_Snippets_CLR_System/system.globalization.numberformatinfo.class/cs/properties2.cs#3)] |
250 |
| - [!code-vb[System.Globalization.NumberFormatInfo.Class#3](~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.globalization.numberformatinfo.class/vb/properties2.vb#3)] |
251 |
| - |
| 248 | + |
| 249 | + :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.globalization.numberformatinfo.class/cs/properties2.cs" interactive="try-dotnet" id="Snippet3"::: |
| 250 | + :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.globalization.numberformatinfo.class/vb/properties2.vb" id="Snippet3"::: |
| 251 | +
|
252 | 252 | Each standard numeric format string uses one or more <xref:System.Globalization.NumberFormatInfo> properties to determine the pattern or the symbols used in the result string. Similarly, each custom numeric format specifier except "0" and "#" insert symbols in the result string that are defined by <xref:System.Globalization.NumberFormatInfo> properties. The following table lists the standard and custom numeric format specifiers and their associated <xref:System.Globalization.NumberFormatInfo> properties. To change the appearance of the result string for a particular culture, see the [Modifying NumberFormatInfo properties](#modifying) section. For details about the use of these format specifiers, see [Standard Numeric Format Strings](/dotnet/standard/base-types/standard-numeric-format-strings) and [Custom Numeric Format Strings](/dotnet/standard/base-types/custom-numeric-format-strings).
|
253 | 253 |
|
254 | 254 | |Format specifier|Associated properties|
|
|
288 | 288 | <a name="currency"></a>
|
289 | 289 | ### Modifying the currency symbol and pattern
|
290 | 290 | The following example modifies a <xref:System.Globalization.NumberFormatInfo> object that represents the formatting conventions of the en-US culture. It assigns the ISO-4217 currency symbol to the <xref:System.Globalization.NumberFormatInfo.CurrencySymbol%2A> property and defines a pattern for currency values that consists of the currency symbol followed by a space and a numeric value.
|
291 |
| - |
292 |
| - [!code-csharp[System.Globalization.NumberFormatInfo.Customize#1](~/samples/snippets/csharp/VS_Snippets_CLR_System/system.globalization.numberformatinfo.customize/cs/customize_currency1.cs#1)] |
293 |
| - [!code-vb[System.Globalization.NumberFormatInfo.Customize#1](~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.globalization.numberformatinfo.customize/vb/customize_currency1.vb#1)] |
294 |
| - |
| 291 | + |
| 292 | + :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.globalization.numberformatinfo.customize/cs/customize_currency1.cs" interactive="try-dotnet" id="Snippet1"::: |
| 293 | + :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.globalization.numberformatinfo.customize/vb/customize_currency1.vb" id="Snippet1"::: |
| 294 | +
|
295 | 295 | <a name="id"></a>
|
296 | 296 | ### Formatting a national identification number
|
297 | 297 | Many national identification numbers consist exclusively of digits and so can easily be formatted by modifying the properties of a <xref:System.Globalization.NumberFormatInfo> object. For example, a social security number in the United States consists of 9 digits arranged as follows: `XXX-XX-XXXX`. The following example assumes that social security numbers are stored as integer values and formats them appropriately.
|
298 |
| - |
299 |
| - [!code-csharp[System.Globalization.NumberFormatInfo.Customize#2](~/samples/snippets/csharp/VS_Snippets_CLR_System/system.globalization.numberformatinfo.customize/cs/customize_ssn1.cs#2)] |
300 |
| - [!code-vb[System.Globalization.NumberFormatInfo.Customize#2](~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.globalization.numberformatinfo.customize/vb/customize_ssn1.vb#2)] |
301 |
| - |
| 298 | + |
| 299 | + :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.globalization.numberformatinfo.customize/cs/customize_ssn1.cs" interactive="try-dotnet" id="Snippet2"::: |
| 300 | + :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.globalization.numberformatinfo.customize/vb/customize_ssn1.vb" id="Snippet2"::: |
| 301 | +
|
302 | 302 | <a name="parsing"></a>
|
303 | 303 | ## Parsing numeric strings
|
304 | 304 | Parsing involves converting the string representation of a number to a number. Each numeric type in the .NET Framework includes two overloaded parsing methods: `Parse` and `TryParse`. The `Parse` method converts a string to a number and throws an exception if the conversion fails. The `TryParse` method converts a string to a number, assigns the number to an `out` argument, and returns a <xref:System.Boolean> value that indicates whether the conversion succeeded.
|
|
308 | 308 | The parsing methods also implicitly or explicitly use a <xref:System.Globalization.NumberFormatInfo> object that defines the specific symbols and patterns that can occur in the string to be parsed. If a <xref:System.Globalization.NumberFormatInfo> object is not provided, the default is the <xref:System.Globalization.NumberFormatInfo> for the current thread culture. For more information about parsing, see the individual parsing methods, such as <xref:System.Int16.Parse%28System.String%29?displayProperty=nameWithType>, <xref:System.Int32.Parse%28System.String%2CSystem.Globalization.NumberStyles%29?displayProperty=nameWithType>, <xref:System.Int64.Parse%28System.String%2CSystem.IFormatProvider%29?displayProperty=nameWithType>, <xref:System.Decimal.Parse%28System.String%2CSystem.Globalization.NumberStyles%2CSystem.IFormatProvider%29?displayProperty=nameWithType>, <xref:System.Double.TryParse%28System.String%2CSystem.Double%40%29?displayProperty=nameWithType>, and <xref:System.Numerics.BigInteger.TryParse%28System.String%2CSystem.Globalization.NumberStyles%2CSystem.IFormatProvider%2CSystem.Numerics.BigInteger%40%29?displayProperty=nameWithType>.
|
309 | 309 |
|
310 | 310 | The following example illustrates the culture-sensitive nature of parsing strings. It tries to parse a string that include thousands separators by using the conventions of the en-US, fr-FR, and invariant cultures. A string that includes the comma as a group separator and the period as a decimal separator fails to parse in the fr-FR culture, and a string with white space as a group separator and a comma as a decimal separator fails to parse in the en-US and invariant cultures.
|
311 |
| - |
312 |
| - [!code-csharp[System.Globalization.NumberFormatInfo.Class#4](~/samples/snippets/csharp/VS_Snippets_CLR_System/system.globalization.numberformatinfo.class/cs/parse1.cs#4)] |
313 |
| - [!code-vb[System.Globalization.NumberFormatInfo.Class#4](~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.globalization.numberformatinfo.class/vb/parse1.vb#4)] |
314 |
| - |
| 311 | + |
| 312 | + :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.globalization.numberformatinfo.class/cs/parse1.cs" interactive="try-dotnet" id="Snippet4"::: |
| 313 | + :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.globalization.numberformatinfo.class/vb/parse1.vb" id="Snippet4"::: |
| 314 | + |
315 | 315 | Parsing generally occurs in two contexts:
|
316 | 316 |
|
317 | 317 | - As an operation that is designed to convert user input into a numeric value.
|
|
325 | 325 | When you are parsing numeric strings input by the user, you should always instantiate a <xref:System.Globalization.NumberFormatInfo> object that reflects the user's cultural settings. For information about how to instantiate a <xref:System.Globalization.NumberFormatInfo> object that reflects user customizations, see the [NumberFormatInfo and dynamic data](#dynamic) section.
|
326 | 326 |
|
327 | 327 | The following example illustrates the difference between a parsing operation that reflects user cultural settings and one that does not. In this case, the default system culture is en-US, but the user has defined "," as the decimal symbol and "." as the group separator in Control Panel, **Region and Language**. Ordinarily, these symbols are reversed in the default en-US culture. When the user enters a string that reflects user settings, and the string is parsed by a <xref:System.Globalization.NumberFormatInfo> object that also reflects user settings (overrides), the parsing operation returns a correct result. However, when the string is parsed by a <xref:System.Globalization.NumberFormatInfo> object that reflects standard en-US cultural settings, it mistakes the comma symbol for a group separator and returns an incorrect result.
|
328 |
| - |
329 |
| - [!code-csharp[System.Globalization.NumberFormatInfo.Class#5](~/samples/snippets/csharp/VS_Snippets_CLR_System/system.globalization.numberformatinfo.class/cs/parseuser1.cs#5)] |
330 |
| - [!code-vb[System.Globalization.NumberFormatInfo.Class#5](~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.globalization.numberformatinfo.class/vb/parseuser1.vb#5)] |
331 |
| - |
| 328 | + |
| 329 | + :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.globalization.numberformatinfo.class/cs/parseuser1.cs" interactive="try-dotnet" id="Snippet5"::: |
| 330 | + :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.globalization.numberformatinfo.class/vb/parseuser1.vb" id="Snippet5"::: |
| 331 | +
|
332 | 332 | <a name="ParseSerial"></a>
|
333 | 333 | ### Serializing and deserializing numeric data
|
334 | 334 | When numeric data is serialized in string format and later deserialized and parsed, the strings should be generated and parsed by using the conventions of the invariant culture. The formatting and parsing operations should never reflect the conventions of a specific culture. If culture-specific settings are used, the portability of the data is strictly limited; it can be successfully deserialized only on a thread whose culture-specific settings are identical to those of the thread on which it was serialized. In some cases, this means that the data cannot even be successfully deserialized on the same system on which it was serialized.
|
335 | 335 |
|
336 | 336 | The following example illustrates what can happen when this principle is violated. Floating-point values in an array are converted to strings when the current thread uses the culture-specific settings of the en-US culture. The data is then parsed by a thread that uses the culture-specific settings of the en-GB culture. In this case, although each parsing operation succeeds, the data does not round-trip successfully and data corruption occurs. In other cases, a parsing operation could fail and a <xref:System.FormatException> exception could be thrown.
|
337 | 337 |
|
338 |
| - [!code-csharp[System.Globalization.NumberFormatInfo.Class#6](~/samples/snippets/csharp/VS_Snippets_CLR_System/system.globalization.numberformatinfo.class/cs/parsepersisted.cs#6)] |
339 |
| - [!code-vb[System.Globalization.NumberFormatInfo.Class#6](~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.globalization.numberformatinfo.class/vb/parsepersisted.vb#6)] |
340 |
| - |
341 |
| - |
| 338 | + :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.globalization.numberformatinfo.class/cs/parsepersisted.cs" interactive="try-dotnet" id="Snippet6"::: |
| 339 | + :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.globalization.numberformatinfo.class/vb/parsepersisted.vb" id="Snippet6"::: |
342 | 340 |
|
| 341 | +
|
| 342 | + |
343 | 343 | ## Examples
|
344 | 344 | The following example shows how to retrieve a <xref:System.Globalization.NumberFormatInfo> object for a corresponding <xref:System.Globalization.CultureInfo> object, and use the retrieved object to query number formatting information for the particular culture.
|
345 | 345 |
|
|
0 commit comments