当前位置: 首页>>代码示例>>C#>>正文


C# Convert.ToString方法代码示例

本文整理汇总了C#中System.Convert.ToString方法的典型用法代码示例。如果您正苦于以下问题:C# Convert.ToString方法的具体用法?C# Convert.ToString怎么用?C# Convert.ToString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Convert的用法示例。


在下文中一共展示了Convert.ToString方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: foreach

int[] bases = { 2, 8, 10, 16};
short[] numbers = { Int16.MinValue, -13621, -18, 12, 19142, Int16.MaxValue };

foreach (int baseValue in bases)
{
   Console.WriteLine("Base {0} conversion:", baseValue);
   foreach (short number in numbers)
   {
      Console.WriteLine("   {0,-8}  -->  0x{1}", 
                        number, Convert.ToString(number, baseValue));
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:12,代码来源:Convert.ToString

输出:

Base 2 conversion:
-32768    -->  0x1000000000000000
-13621    -->  0x1100101011001011
-18       -->  0x1111111111101110
12        -->  0x1100
19142     -->  0x100101011000110
32767     -->  0x111111111111111
Base 8 conversion:
-32768    -->  0x100000
-13621    -->  0x145313
-18       -->  0x177756
12        -->  0x14
19142     -->  0x45306
32767     -->  0x77777
Base 10 conversion:
-32768    -->  0x-32768
-13621    -->  0x-13621
-18       -->  0x-18
12        -->  0x12
19142     -->  0x19142
32767     -->  0x32767
Base 16 conversion:
-32768    -->  0x8000
-13621    -->  0xcacb
-18       -->  0xffee
12        -->  0xc
19142     -->  0x4ac6
32767     -->  0x7fff

示例2: foreach

short[] numbers = { Int16.MinValue, Int16.MaxValue};
System.Globalization.NumberFormatInfo nfi = new System.Globalization.NumberFormatInfo();
nfi.NegativeSign = "~";
nfi.PositiveSign = "!";

foreach (short number in numbers)
   Console.WriteLine("{0,-8}  -->  {1,8}", 
                     Convert.ToString(number, System.Globalization.CultureInfo.InvariantCulture), 
                     Convert.ToString(number, nfi));
开发者ID:.NET开发者,项目名称:System,代码行数:9,代码来源:Convert.ToString

输出:

-32768    -->    ~32768
32767     -->     32767

示例3:

ulong number = UInt64.MaxValue;
System.Globalization.NumberFormatInfo nfi = new System.Globalization.NumberFormatInfo();
nfi.NegativeSign = "~";
nfi.PositiveSign = "!";

Console.WriteLine("{0,-12}  -->  {1,12}",
                  Convert.ToString(number, System.Globalization.CultureInfo.InvariantCulture),
                  Convert.ToString(number, nfi));
开发者ID:.NET开发者,项目名称:System,代码行数:8,代码来源:Convert.ToString

输出:

18446744073709551615  -->  18446744073709551615

示例4: foreach

// Define an array of numbers to display.
decimal[] numbers = { 1734231911290.16m, -17394.32921m,
                      3193.23m, 98012368321.684m };
// Define the culture names used to display them.
string[] cultureNames = { "en-US", "fr-FR", "ja-JP", "ru-RU" };

foreach (decimal number in numbers)
{
   Console.WriteLine("{0}:", Convert.ToString(number,
                             System.Globalization.CultureInfo.InvariantCulture));
   foreach (string cultureName in cultureNames)
   {
      System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo(cultureName);
      Console.WriteLine("   {0}: {1,20}",
                        culture.Name, Convert.ToString(number, culture));
   }
   Console.WriteLine();
}
开发者ID:.NET开发者,项目名称:System,代码行数:18,代码来源:Convert.ToString

输出:

1734231911290.16:
en-US:     1734231911290.16
fr-FR:     1734231911290,16
ja-JP:     1734231911290.16
ru-RU:     1734231911290,16

-17394.32921:
en-US:         -17394.32921
fr-FR:         -17394,32921
ja-JP:         -17394.32921
ru-RU:         -17394,32921

3193.23:
en-US:              3193.23
fr-FR:              3193,23
ja-JP:              3193.23
ru-RU:              3193,23

98012368321.684:
en-US:      98012368321.684
fr-FR:      98012368321,684
ja-JP:      98012368321.684
ru-RU:      98012368321,684

示例5: DateTime

// Specify the date to be formatted using various cultures.
DateTime tDate = new DateTime(2010, 4, 15, 20, 30, 40, 333);
// Specify the cultures.
string[] cultureNames = { "en-US", "es-AR", "fr-FR", "hi-IN",
                          "ja-JP", "nl-NL", "ru-RU", "ur-PK" };

Console.WriteLine("Converting the date {0}: ", 
                  Convert.ToString(tDate, 
                          System.Globalization.CultureInfo.InvariantCulture));

foreach (string cultureName in cultureNames)
{
   System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo(cultureName);
   string dateString = Convert.ToString(tDate, culture);
   Console.WriteLine("   {0}:  {1,-12}", 
                     culture.Name, dateString);
}
开发者ID:.NET开发者,项目名称:System,代码行数:17,代码来源:Convert.ToString

输出:

Converting the date 04/15/2010 20:30:40:
en-US:  4/15/2010 8:30:40 PM
es-AR:  15/04/2010 08:30:40 p.m.
fr-FR:  15/04/2010 20:30:40
hi-IN:  15-04-2010 20:30:40
ja-JP:  2010/04/15 20:30:40
nl-NL:  15-4-2010 20:30:40
ru-RU:  15.04.2010 20:30:40
ur-PK:  15/04/2010 8:30:40 PM

示例6: foreach

int[] numbers = { Int32.MinValue, Int32.MaxValue};
System.Globalization.NumberFormatInfo nfi = new System.Globalization.NumberFormatInfo();
nfi.NegativeSign = "~";
nfi.PositiveSign = "!";

foreach (int number in numbers)
   Console.WriteLine("{0,-12}  -->  {1,12}", 
                     Convert.ToString(number, System.Globalization.CultureInfo.InvariantCulture), 
                     Convert.ToString(number, nfi));
开发者ID:.NET开发者,项目名称:System,代码行数:9,代码来源:Convert.ToString

输出:

-2147483648  -->  ~2147483648
2147483647  -->  2147483647

示例7: foreach

// Define an array of numbers to display.
double[] numbers = { -1.5345e16, -123.4321, 19092.123, 1.1734231911290e16 };
// Define the culture names used to display them.
string[] cultureNames = { "en-US", "fr-FR", "ja-JP", "ru-RU" };

foreach (double number in numbers)
{
   Console.WriteLine("{0}:", Convert.ToString(number,
                             System.Globalization.CultureInfo.InvariantCulture));
   foreach (string cultureName in cultureNames)
   {
      System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo(cultureName);
      Console.WriteLine("   {0}: {1,20}",
                        culture.Name, Convert.ToString(number, culture));
   }
   Console.WriteLine();
}
开发者ID:.NET开发者,项目名称:System,代码行数:17,代码来源:Convert.ToString

输出:

-1.5345E+16:
en-US:          -1.5345E+16
fr-FR:          -1,5345E+16
ja-JP:          -1.5345E+16
ru-RU:          -1,5345E+16

-123.4321:
en-US:            -123.4321
fr-FR:            -123,4321
ja-JP:            -123.4321
ru-RU:            -123,4321

19092.123:
en-US:            19092.123
fr-FR:            19092,123
ja-JP:            19092.123
ru-RU:            19092,123

1.173423191129E+16:
en-US:   1.173423191129E+16
fr-FR:   1,173423191129E+16
ja-JP:   1.173423191129E+16
ru-RU:   1,173423191129E+16

示例8: foreach

int[] bases = { 2, 8, 10, 16};
int[] numbers = { Int32.MinValue, -19327543, -13621, -18, 12, 
                             19142, Int32.MaxValue };

foreach (int baseValue in bases)
{
   Console.WriteLine("Base {0} conversion:", baseValue);
   foreach (int number in numbers)
   {
      Console.WriteLine("   {0,-15}  -->  0x{1}", 
                        number, Convert.ToString(number, baseValue));
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:13,代码来源:Convert.ToString

输出:

Base 2 conversion:
-2147483648      -->  0x10000000000000000000000000000000
-19327543        -->  0x11111110110110010001010111001001
-13621           -->  0x11111111111111111100101011001011
-18              -->  0x11111111111111111111111111101110
12               -->  0x1100
19142            -->  0x100101011000110
2147483647       -->  0x1111111111111111111111111111111
Base 8 conversion:
-2147483648      -->  0x20000000000
-19327543        -->  0x37666212711
-13621           -->  0x37777745313
-18              -->  0x37777777756
12               -->  0x14
19142            -->  0x45306
2147483647       -->  0x17777777777
Base 10 conversion:
-2147483648      -->  0x-2147483648
-19327543        -->  0x-19327543
-13621           -->  0x-13621
-18              -->  0x-18
12               -->  0x12
19142            -->  0x19142
2147483647       -->  0x2147483647
Base 16 conversion:
-2147483648      -->  0x80000000
-19327543        -->  0xfed915c9
-13621           -->  0xffffcacb
-18              -->  0xffffffee
12               -->  0xc
19142            -->  0x4ac6
2147483647       -->  0x7fffffff

示例9: foreach

sbyte[] numbers = { SByte.MinValue, -12, 17, SByte.MaxValue};
System.Globalization.NumberFormatInfo nfi = new System.Globalization.NumberFormatInfo();
nfi.NegativeSign = "~";
nfi.PositiveSign = "!";
foreach (sbyte number in numbers)
   Console.WriteLine(Convert.ToString(number, nfi));
开发者ID:.NET开发者,项目名称:System,代码行数:6,代码来源:Convert.ToString

输出:

~128
~12
17
127

示例10: foreach

int[] bases = { 2, 8, 10, 16};
long[] numbers = { Int64.MinValue, -193275430, -13621, -18, 12, 
                   1914206117, Int64.MaxValue };

foreach (int baseValue in bases)
{
   Console.WriteLine("Base {0} conversion:", baseValue);
   foreach (long number in numbers)
   {
      Console.WriteLine("   {0,-23}  -->  0x{1}", 
                        number, Convert.ToString(number, baseValue));
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:13,代码来源:Convert.ToString

输出:

Base 2 conversion:
-9223372036854775808     -->  0x1000000000000000000000000000000000000000000000000000000000000000
-193275430               -->  0x1111111111111111111111111111111111110100011110101101100111011010
-13621                   -->  0x1111111111111111111111111111111111111111111111111100101011001011
-18                      -->  0x1111111111111111111111111111111111111111111111111111111111101110
12                       -->  0x1100
1914206117               -->  0x1110010000110000111011110100101
9223372036854775807      -->  0x111111111111111111111111111111111111111111111111111111111111111
Base 8 conversion:
-9223372036854775808     -->  0x1000000000000000000000
-193275430               -->  0x1777777777776436554732
-13621                   -->  0x1777777777777777745313
-18                      -->  0x1777777777777777777756
12                       -->  0x14
1914206117               -->  0x16206073645
9223372036854775807      -->  0x777777777777777777777
Base 10 conversion:
-9223372036854775808     -->  0x-9223372036854775808
-193275430               -->  0x-193275430
-13621                   -->  0x-13621
-18                      -->  0x-18
12                       -->  0x12
1914206117               -->  0x1914206117
9223372036854775807      -->  0x9223372036854775807
Base 16 conversion:
-9223372036854775808     -->  0x8000000000000000
-193275430               -->  0xfffffffff47ad9da
-13621                   -->  0xffffffffffffcacb
-18                      -->  0xffffffffffffffee
12                       -->  0xc
1914206117               -->  0x721877a5
9223372036854775807      -->  0x7fffffffffffffff

示例11: Temperature

//引入命名空间
using System;

public class Temperature
{
   private decimal m_Temp;

   public Temperature(decimal temperature)
   {
      this.m_Temp = temperature;
   }
   
   public decimal Celsius
   {
      get { return this.m_Temp; }   
   }
   
   public decimal Kelvin
   {
      get { return this.m_Temp + 273.15m; }    
   }
   
   public decimal Fahrenheit
   {
      get { return Math.Round((decimal) (this.m_Temp * 9 / 5 + 32), 2); }
   }
   
   public override string ToString()
   {
      return m_Temp.ToString("N2") + " °C";
   }
}

public class Example
{
   public static void Main()
   {
      Temperature cold = new Temperature(-40);
      Temperature freezing = new Temperature(0);
      Temperature boiling = new Temperature(100);
      
      Console.WriteLine(Convert.ToString(cold, null));
      Console.WriteLine(Convert.ToString(freezing, null));
      Console.WriteLine(Convert.ToString(boiling, null));
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:46,代码来源:Convert.ToString

输出:

-40.00 °C
0.00 °C
100.00 °C

示例12: Temperature

//引入命名空间
using System;

public class Temperature : IFormattable 
{
   private decimal m_Temp; 

   public Temperature(decimal temperature)
   {
      this.m_Temp = temperature;
   } 

   public decimal Celsius 
   { get { return this.m_Temp; } } 

   public decimal Kelvin 
   { get { return this.m_Temp + 273.15m; } }   

   public decimal Fahrenheit
   {  get { return Math.Round(this.m_Temp * 9m / 5m + 32m, 2); } }

   public override String ToString() 
   { 
      return ToString("G", null); 
   } 
   
   public String ToString(String fmt, IFormatProvider provider) 
   {
      TemperatureProvider formatter = null;
      if (provider != null) 
         formatter = provider.GetFormat(typeof(TemperatureProvider)) 
                                       as TemperatureProvider;

      if (String.IsNullOrWhiteSpace(fmt)) {
         if (formatter != null) 
            fmt = formatter.Format;
         else
            fmt = "G";
      }

      switch (fmt.ToUpper()) {
         case "G":
         case "C":
            return m_Temp.ToString("N2") + " °C"; 
         case "F":
            return Fahrenheit.ToString("N2") + " °F";
         case "K":
            return Kelvin.ToString("N2") + " K";
         default:
            throw new FormatException(String.Format("'{0}' is not a valid format specifier.", fmt));
      }
   }                             
} 

public class TemperatureProvider : IFormatProvider
{
   private String[] fmtStrings = { "C", "G", "F", "K" };
   private Random rnd = new Random();
   
   public Object GetFormat(Type formatType) 
   { 
      return this; 
   }
   
   public String Format
   { get { return fmtStrings[rnd.Next(0, fmtStrings.Length)]; } }
}

public class Example
{
   public static void Main()
   {
      Temperature cold = new Temperature (-40);
      Temperature freezing = new Temperature (0);
      Temperature boiling = new Temperature (100);

      TemperatureProvider tp = new TemperatureProvider();
      
      Console.WriteLine(Convert.ToString(cold, tp));
      Console.WriteLine(Convert.ToString(freezing, tp));
      Console.WriteLine(Convert.ToString(boiling, tp));
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:83,代码来源:Convert.ToString

输出:

-40.00 °C
273.15 K
100.00 °C

示例13: foreach

// Define an array of numbers to display.
float[] numbers = { -1.5345e16f, -123.4321f, 19092.123f, 1.1734231911290e16f };
// Define the culture names used to display them.
string[] cultureNames = { "en-US", "fr-FR", "ja-JP", "ru-RU" };

foreach (float number in numbers)
{
   Console.WriteLine("{0}:", Convert.ToString(number,
                             System.Globalization.CultureInfo.InvariantCulture));
   foreach (string cultureName in cultureNames)
   {
      System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo(cultureName);
      Console.WriteLine("   {0}: {1,20}",
                        culture.Name, Convert.ToString(number, culture));
   }
   Console.WriteLine();
}
开发者ID:.NET开发者,项目名称:System,代码行数:17,代码来源:Convert.ToString

输出:

-1.5345E+16:
en-US:          -1.5345E+16
fr-FR:          -1,5345E+16
ja-JP:          -1.5345E+16
ru-RU:          -1,5345E+16

-123.4321:
en-US:            -123.4321
fr-FR:            -123,4321
ja-JP:            -123.4321
ru-RU:            -123,4321

19092.123:
en-US:            19092.123
fr-FR:            19092,123
ja-JP:            19092.123
ru-RU:            19092,123

1.173423191129E+16:
en-US:   1.173423191129E+16
fr-FR:   1,173423191129E+16
ja-JP:   1.173423191129E+16
ru-RU:   1,173423191129E+16

示例14: GetFormat

// Example of Convert.ToString( non-numeric types, IFormatProvider ).
using System;
using System.Globalization;

// An instance of this class can be passed to methods that require 
// an IFormatProvider.
public class DummyProvider : IFormatProvider
{
    // Normally, GetFormat returns an object of the requested type
    // (usually itself) if it is able; otherwise, it returns Nothing. 
    public object GetFormat( Type argType )
    {
        // Here, the type of argType is displayed, and GetFormat
        // always returns Nothing.
        Console.Write( "{0,-40}", argType.ToString( ) );
        return null;
    }
}

class ConvertNonNumericProviderDemo
{
    static void Main( )
    {
        // Create an instance of the IFormatProvider.
        DummyProvider provider = new DummyProvider( );
        string converted;

        // Convert these values using DummyProvider.
        int      Int32A     = -252645135;
        double   DoubleA    = 61680.3855;
        object   ObjDouble  = (object)( -98765.4321 );
        DateTime DayTimeA   = new DateTime( 2001, 9, 11, 13, 45, 0 );

        bool     BoolA      = true;
        string   StringA    = "Qwerty";
        char     CharA      = '$';
        TimeSpan TSpanA     = new TimeSpan( 0, 18, 0 );
        object   ObjOther   = (object)provider;

        Console.WriteLine( "This example of " +
            "Convert.ToString( non-numeric, IFormatProvider ) \n" +
            "generates the following output. The provider type, " +
            "argument type, \nand argument value are displayed." );
        Console.WriteLine( "\nNote: The IFormatProvider object is " +
            "not called for Boolean, String, \nChar, TimeSpan, " +
            "and non-numeric Object." );

        // The format provider is called for these conversions.
        Console.WriteLine( );
        converted = Convert.ToString( Int32A, provider );
        Console.WriteLine( "int      {0}", converted );
        converted = Convert.ToString( DoubleA, provider );
        Console.WriteLine( "double   {0}", converted );
        converted = Convert.ToString( ObjDouble, provider );
        Console.WriteLine( "object   {0}", converted );
        converted = Convert.ToString( DayTimeA, provider );
        Console.WriteLine( "DateTime {0}", converted );

        // The format provider is not called for these conversions.
        Console.WriteLine( );
        converted = Convert.ToString( BoolA, provider );
        Console.WriteLine( "bool     {0}", converted );
        converted = Convert.ToString( StringA, provider );
        Console.WriteLine( "string   {0}", converted );
        converted = Convert.ToString( CharA, provider );
        Console.WriteLine( "char     {0}", converted );
        converted = Convert.ToString( TSpanA, provider );
        Console.WriteLine( "TimeSpan {0}", converted );
        converted = Convert.ToString( ObjOther, provider );
        Console.WriteLine( "object   {0}", converted );
    }
}

/*
This example of Convert.ToString( non-numeric, IFormatProvider )
generates the following output. The provider type, argument type,
and argument value are displayed.

Note: The IFormatProvider object is not called for Boolean, String,
Char, TimeSpan, and non-numeric Object.

System.Globalization.NumberFormatInfo   int      -252645135
System.Globalization.NumberFormatInfo   double   61680.3855
System.Globalization.NumberFormatInfo   object   -98765.4321
System.Globalization.DateTimeFormatInfo DateTime 9/11/2001 1:45:00 PM

bool     True
string   Qwerty
char     $
TimeSpan 00:18:00
object   DummyProvider

*/
开发者ID:.NET开发者,项目名称:System,代码行数:93,代码来源:Convert.ToString

示例15:

ushort number = UInt16.MaxValue;
System.Globalization.NumberFormatInfo nfi = new System.Globalization.NumberFormatInfo();
nfi.NegativeSign = "~";
nfi.PositiveSign = "!";

Console.WriteLine("{0,-6}  -->  {1,6}",
                  Convert.ToString(number, System.Globalization.CultureInfo.InvariantCulture),
                  Convert.ToString(number, nfi));
开发者ID:.NET开发者,项目名称:System,代码行数:8,代码来源:Convert.ToString

输出:

65535   -->   65535


注:本文中的System.Convert.ToString方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。