當前位置: 首頁>>代碼示例>>C#>>正文


C# FormatException類代碼示例

本文整理匯總了C#中System.FormatException的典型用法代碼示例。如果您正苦於以下問題:C# FormatException類的具體用法?C# FormatException怎麽用?C# FormatException使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


FormatException類屬於System命名空間,在下文中一共展示了FormatException類的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: Main

//引入命名空間
using System;

public class Example
{
   public static void Main()
   {
      decimal price = 169.32m;
      Console.WriteLine("The cost is {0:Q2}.", price);
   }
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:11,代碼來源:FormatException

輸出:

Unhandled Exception: System.FormatException: Format specifier was invalid.
at System.Number.FormatDecimal(Decimal value, String format, NumberFormatInfo info)
at System.Decimal.ToString(String format, IFormatProvider provider)
at System.Text.StringBuilder.AppendFormat(IFormatProvider provider, String format, Object[] args)
at System.IO.TextWriter.WriteLine(String format, Object arg0)
at System.IO.TextWriter.SyncTextWriter.WriteLine(String format, Object arg0)
at Example.Main()

示例2: Main

//引入命名空間
using System;

public class Example
{
   public static void Main()
   {
      decimal price = 169.32m;
      Console.WriteLine("The cost is {0:C2}.", price);
   }
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:11,代碼來源:FormatException

輸出:

The cost is $169.32.

示例3: Main

//引入命名空間
using System;

public class Example
{
   public static void Main()
   {
      string guidString = "ba748d5c-ae5f-4cca-84e5-1ac5291c38cb";
      Console.WriteLine(Guid.ParseExact(guidString, "G"));
   }
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:11,代碼來源:FormatException

輸出:

Unhandled Exception: System.FormatException: 
Format String can be only "D", "d", "N", "n", "P", "p", "B", "b", "X" or "x".
at System.Guid.ParseExact(String input, String format)
at Example.Main()

示例4: Main

//引入命名空間
using System;

public class Example
{
   public static void Main()
   {
      string guidString = "ba748d5c-ae5f-4cca-84e5-1ac5291c38cb";
      Console.WriteLine(Guid.Parse(guidString));
   }
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:11,代碼來源:FormatException

輸出:

ba748d5c-ae5f-4cca-84e5-1ac5291c38cb

示例5: Main

//引入命名空間
using System;

public class Example
{
   public enum TemperatureScale 
   { Celsius, Fahrenheit, Kelvin }

   public static void Main()
   {
      String info = GetCurrentTemperature();
      Console.WriteLine(info);
   }

   private static String GetCurrentTemperature()
   {
      DateTime dat = DateTime.Now;
      Decimal temp = 20.6m;
      TemperatureScale scale = TemperatureScale.Celsius;
      String result;
      
      result = String.Format("At {0:t} on {1:D}, the temperature is {2:F1} {3:G}",
                             dat, temp, scale);    
      return result;
   }
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:26,代碼來源:FormatException

輸出:

Unhandled Exception: System.FormatException: Format specifier was invalid.
at System.Number.FormatDecimal(Decimal value, String format, NumberFormatInfo info)
at System.Decimal.ToString(String format, IFormatProvider provider)
at System.Text.StringBuilder.AppendFormat(IFormatProvider provider, String format, Object[] args)
at System.String.Format(IFormatProvider provider, String format, Object[] args)
at Example.Main()

示例6: Main

//引入命名空間
using System;

public class Example
{
   public enum TemperatureScale 
   { Celsius, Fahrenheit, Kelvin }

   public static void Main()
   {
      String info = GetCurrentTemperature();
      Console.WriteLine(info);
   }

   private static String GetCurrentTemperature()
   {
      DateTime dat = DateTime.Now;
      Decimal temp = 20.6m;
      TemperatureScale scale = TemperatureScale.Celsius;
      String result;
      
      result = String.Format("At {0:t} on {0:D}, the temperature is {1:F1} {2:G}",
                             dat, temp, scale);    
      return result;
   }
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:26,代碼來源:FormatException

輸出:

At 10:40 AM on Wednesday, June 04, 2014, the temperature is 20.6 Celsius

示例7:

string result;
int nOpen = 1;
int nClose = 2;
result = String.Format("The text has {0} '{{' characters and {1} '}}' characters.",
                       nOpen, nClose);
Console.WriteLine(result);
開發者ID:.NET開發者,項目名稱:System,代碼行數:6,代碼來源:FormatException

示例8:

int n1 = 10;
int n2 = 20;
String result = String.Format("{0 + {1] = {2}", 
                              n1, n2, n1 + n2);
開發者ID:.NET開發者,項目名稱:System,代碼行數:4,代碼來源:FormatException

示例9: Main

//引入命名空間
using System;
using System.Collections.Generic;

public class Example
{
   public static void Main()
   {
      Random rnd = new Random();
      int[]  numbers = new int[4];
      int total = 0;
      for (int ctr = 0; ctr <= 2; ctr++) {
         int number = rnd.Next(1001);
         numbers[ctr] = number;
         total += number;
      }   
      numbers[3] = total;
      Console.WriteLine("{0} + {1} + {2} = {3}", numbers);   
   }
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:20,代碼來源:FormatException

輸出:

Unhandled Exception: 
System.FormatException: 
Index (zero based) must be greater than or equal to zero and less than the size of the argument list.
at System.Text.StringBuilder.AppendFormat(IFormatProvider provider, String format, Object[] args)
at System.IO.TextWriter.WriteLine(String format, Object arg0)
at System.IO.TextWriter.SyncTextWriter.WriteLine(String format, Object arg0)
at Example.Main()

示例10: Main

//引入命名空間
using System;
using System.Collections.Generic;

public class Example
{
   public static void Main()
   {
      Random rnd = new Random();
      int[]  numbers = new int[4];
      int total = 0;
      for (int ctr = 0; ctr <= 2; ctr++) {
         int number = rnd.Next(1001);
         numbers[ctr] = number;
         total += number;
      }   
      numbers[3] = total;
      object[] values = new object[numbers.Length];
      numbers.CopyTo(values, 0);
      Console.WriteLine("{0} + {1} + {2} = {3}", values);   
   }
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:22,代碼來源:FormatException

輸出:

477 + 956 + 901 = 2334


注:本文中的System.FormatException類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。