The example catches the two most common exceptions that can be thrown by this method, xref:System.FormatException and xref:System.OverflowException. The following example calls the xref:32%28System.String%29?displayProperty=nameWithType method to convert an input string to an int. The following table lists some of the methods from the xref:System.Convert class that you can use to convert a string to a number. For a larger string, the xref: class can be used instead. Because the strings to be parsed contain a few characters, the example calls the xref:%2A?displayProperty=nameWithType method to assign valid characters to a new string. It assigns valid characters from the beginning of a string to a new string before calling the xref:%2A method. The following example illustrates one approach to parsing a string expected to include leading numeric characters (including hexadecimal characters) and trailing non-numeric characters. The following example demonstrates both successful and unsuccessful calls to Parse and TryParse. You can check for a null or empty string before attempting to parse it by calling the xref:%2A?displayProperty=nameWithType method. A string whose value is null or xref:?displayProperty=nameWithType fails to parse successfully. 3" (note the embedded space), "10e1" ( float.TryParse works here), and so on. For example, you can use decimal.TryParse to parse "10", "10.3", or " 10 ", but you can't use this method to parse 10 from "10X", "1 0" (note the embedded space), "10. Any white space within the string that forms the number causes an error. The Parse and TryParse methods ignore white space at the beginning and at the end of the string, but all other characters must be characters that form the appropriate numeric type ( int, long, ulong, float, decimal, and so on). When calling a Parse method, you should always use exception handling to catch a xref:System.FormatException when the parse operation fails. If the string isn't in a valid format, Parse throws an exception, but TryParse returns false. The Parse method returns the converted number the TryParse method returns a boolean value that indicates whether the conversion succeeded, and returns the converted number in an out parameter. The xref:32%2A?displayProperty=nameWithType method uses xref:%2A internally. You use Parse or TryParse methods on the numeric type you expect the string contains, such as the xref:System.Int32?displayProperty=nameWithType type. Using a xref:System.Convert method is more useful for general objects that implement xref:System.IConvertible. It's slightly more efficient and straightforward to call a TryParse method (for example, int.TryParse("11", out number)) or Parse method (for example, var number = int.Parse("11")). You convert a string to a number by calling the Parse or TryParse method found on numeric types ( int, long, double, and so on), or by using methods in the xref:System.Convert?displayProperty=nameWithType class. How to convert a string to a number (C# Programming Guide)
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |