此主题尚未评级 - 评价此主题

Convert.ToString 方法 (UInt16, IFormatProvider)

2013/12/13

将指定的 16 位无符号整数的值转换为它的等效 String 表示形式。

此 API 不符合 CLS。 

Namespace:  System
程序集:  mscorlib(位于 mscorlib.dll 中)
[CLSCompliantAttribute(false)]
public static string ToString(
	ushort value,
	IFormatProvider provider
)

参数

value
类型: System.UInt16
16 位无符号整数。
provider
类型: System.IFormatProvider
IFormatProvider 接口实现,提供区域性特定的格式设置信息。

返回值

类型: System.String
等效于 value 的值的 String

此实现与 UInt16.ToString 相同。

下面的代码示例使用 ToString 方法,通过 IFormatProvider 对象,将 16 位无符号整数转换为 String


// Example of the Convert.ToString( numeric types ) and 
// Convert.ToString( numeric types, IFormatProvider ) methods.
using System;
using System.Globalization;

class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      // Create a NumberFormatInfo object and set several of its
      // properties that apply to numbers.
      NumberFormatInfo provider = new NumberFormatInfo();
      string formatter = "{0,22}   {1}";

      // These properties will affect the conversion.
      provider.NegativeSign = "minus ";
      provider.NumberDecimalSeparator = " point ";

      // These properties will not be applied.
      provider.NumberDecimalDigits = 2;
      provider.NumberGroupSeparator = ".";
      provider.NumberGroupSizes = new int[] { 3 };

      // Convert these values using default values and the
      // format provider created above.
      byte ByteA = 140;
      SByte SByteA = -60;
      UInt16 UInt16A = 61680;
      short Int16A = -3855;

      UInt32 UInt32A = 4042322160;
      int Int32A = -252645135;
      UInt64 UInt64A = 8138269444283625712;
      long Int64A = -1085102592571150095;

      float SingleA = -32.375F;
      double DoubleA = 61680.3855;
      decimal DecimA = 4042322160.252645135M;
      object ObjDouble = (object)(-98765.4321);

      outputBlock.Text += "This example of " +
          "Convert.ToString( numeric types ) and \n" +
          "Convert.ToString( numeric types, IFormatProvider ) \n" +
          "converts values of each of the CLR base numeric types " +
          "to strings, \nusing default formatting and a " +
          "NumberFormatInfo object." + "\n";
      outputBlock.Text +=
          "\nNote: Of the several NumberFormatInfo " +
          "properties that are changed, \nonly the negative sign " +
          "and decimal separator affect the conversions.\n" + "\n";
      outputBlock.Text += String.Format(formatter, "Default", "Format Provider") + "\n";
      outputBlock.Text += String.Format(formatter, "-------", "---------------") + "\n";

      // Convert the values with and without a format provider.
      outputBlock.Text += String.Format(formatter, Convert.ToString(ByteA),
          Convert.ToString(ByteA, provider)) + "\n";
      outputBlock.Text += String.Format(formatter, Convert.ToString(SByteA),
          Convert.ToString(SByteA, provider)) + "\n";
      outputBlock.Text += String.Format(formatter, Convert.ToString(UInt16A),
          Convert.ToString(UInt16A, provider)) + "\n";
      outputBlock.Text += String.Format(formatter, Convert.ToString(Int16A),
          Convert.ToString(Int16A, provider)) + "\n";

      outputBlock.Text += String.Format(formatter, Convert.ToString(UInt32A),
          Convert.ToString(UInt32A, provider)) + "\n";
      outputBlock.Text += String.Format(formatter, Convert.ToString(Int32A),
          Convert.ToString(Int32A, provider)) + "\n";
      outputBlock.Text += String.Format(formatter, Convert.ToString(UInt64A),
          Convert.ToString(UInt64A, provider)) + "\n";
      outputBlock.Text += String.Format(formatter, Convert.ToString(Int64A),
          Convert.ToString(Int64A, provider)) + "\n";

      outputBlock.Text += String.Format(formatter, Convert.ToString(SingleA),
          Convert.ToString(SingleA, provider)) + "\n";
      outputBlock.Text += String.Format(formatter, Convert.ToString(DoubleA),
          Convert.ToString(DoubleA, provider)) + "\n";
      outputBlock.Text += String.Format(formatter, Convert.ToString(DecimA),
          Convert.ToString(DecimA, provider)) + "\n";
      outputBlock.Text += String.Format(formatter, Convert.ToString(ObjDouble),
          Convert.ToString(ObjDouble, provider)) + "\n";
   }
}

/*
This example of Convert.ToString( numeric types ) and
Convert.ToString( numeric types, IFormatProvider )
converts values of each of the CLR base numeric types to strings,
using default formatting and a NumberFormatInfo object.

Note: Of the several NumberFormatInfo properties that are changed,
only the negative sign and decimal separator affect the conversions.

               Default   Format Provider
               -------   ---------------
                   140   140
                   -60   minus 60
                 61680   61680
                 -3855   minus 3855
            4042322160   4042322160
            -252645135   minus 252645135
   8138269444283625712   8138269444283625712
  -1085102592571150095   minus 1085102592571150095
               -32.375   minus 32 point 375
            61680.3855   61680 point 3855
  4042322160.252645135   4042322160 point 252645135
           -98765.4321   minus 98765 point 4321
*/


Windows Phone OS

受以下版本支持: 8.0, 7.1, 7.0

Windows Phone

本文是否对您有所帮助?
(1500 个剩余字符)
感谢您的反馈
显示:
© 2014 Microsoft. 版权所有。