Advertisement
lil_keeeet

les 7

Oct 24th, 2021
1,057
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.88 KB | None | 0 0
  1. using System;
  2. using System.Globalization;
  3. namespace les7
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             //Example 1
  10.             Console.WriteLine("Example 1");
  11.             // метод Parse используется только для конвертации строк
  12.             string str = "213";
  13.             int a = int.Parse(str);
  14.             Console.WriteLine(a);
  15.  
  16.             //--------------------------------------------------------
  17.             // Исключение на неуспех ковертации
  18.             Console.WriteLine("Example 2");
  19.             try
  20.             {
  21.                 int b = Convert.ToInt32(str);
  22.                 Console.WriteLine("Sucess");
  23.             }
  24.             catch (Exception) {
  25.                 Console.WriteLine("Error.");
  26.             }
  27.  
  28.             //--------------------------------------------------------
  29.             // Парсинг строки с региональной настройкой
  30.             Console.WriteLine("Example 3");
  31.             string str2= "2,3";
  32.             NumberFormatInfo numberFormatInfo = new NumberFormatInfo() {
  33.                 NumberDecimalSeparator = ",",
  34.             };
  35.             double c = double.Parse(str2, numberFormatInfo);
  36.             Console.WriteLine(c);
  37.  
  38.             //--------------------------------------------------------
  39.             // TryParse()
  40.             Console.WriteLine("Example 4");
  41.  
  42.             string str3 = "33";
  43.             int h;
  44.             //TryParse возвращает bool
  45.             bool isParseDone = int.TryParse(str3, out h);
  46.             if (isParseDone)
  47.             {
  48.                 Console.WriteLine("Sucess of working TryParse. h = " + h);
  49.  
  50.             }
  51.             else {
  52.                 Console.WriteLine("Fail of of working TryParse. h = " + h);
  53.             }
  54.         }
  55.     }
  56. }
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement