Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Globalization;
- namespace les7
- {
- class Program
- {
- static void Main(string[] args)
- {
- //Example 1
- Console.WriteLine("Example 1");
- // метод Parse используется только для конвертации строк
- string str = "213";
- int a = int.Parse(str);
- Console.WriteLine(a);
- //--------------------------------------------------------
- // Исключение на неуспех ковертации
- Console.WriteLine("Example 2");
- try
- {
- int b = Convert.ToInt32(str);
- Console.WriteLine("Sucess");
- }
- catch (Exception) {
- Console.WriteLine("Error.");
- }
- //--------------------------------------------------------
- // Парсинг строки с региональной настройкой
- Console.WriteLine("Example 3");
- string str2= "2,3";
- NumberFormatInfo numberFormatInfo = new NumberFormatInfo() {
- NumberDecimalSeparator = ",",
- };
- double c = double.Parse(str2, numberFormatInfo);
- Console.WriteLine(c);
- //--------------------------------------------------------
- // TryParse()
- Console.WriteLine("Example 4");
- string str3 = "33";
- int h;
- //TryParse возвращает bool
- bool isParseDone = int.TryParse(str3, out h);
- if (isParseDone)
- {
- Console.WriteLine("Sucess of working TryParse. h = " + h);
- }
- else {
- Console.WriteLine("Fail of of working TryParse. h = " + h);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement