当前位置: 首页>>代码示例>>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;未经允许,请勿转载。