Exporter (0) Imprimer
Développer tout

Convert.ToString, méthode (UInt16, IFormatProvider)

Convertit la valeur de l'entier non signé 16 bits spécifié en sa représentation String équivalente.

Cette méthode n'est pas conforme CLS.  

Espace de noms : System
Assembly : mscorlib (dans mscorlib.dll)

[CLSCompliantAttribute(false)] 
public static string ToString (
	ushort value,
	IFormatProvider provider
)
/** @attribute CLSCompliantAttribute(false) */ 
public static String ToString (
	UInt16 value, 
	IFormatProvider provider
)
CLSCompliantAttribute(false) 
public static function ToString (
	value : ushort, 
	provider : IFormatProvider
) : String

Paramètres

value

Entier non signé 16 bits.

provider

Implémentation de l'interface IFormatProvider qui fournit des informations de mise en forme propres à la culture.

Valeur de retour

String équivalant à la valeur de value.

Cette implémentation est identique à UInt16.ToString.

L'exemple de code suivant convertit un entier 16 bits non signé en un String avec la méthode ToString, à l'aide d'un objet IFormatProvider.

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

class ConvertNumericProviderDemo
{
    static void Main( )
    {
        // 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 );

        Console.WriteLine( "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." );
        Console.WriteLine(
            "\nNote: Of the several NumberFormatInfo " +
            "properties that are changed, \nonly the negative sign " +
            "and decimal separator affect the conversions.\n" );
        Console.WriteLine( formatter, "Default", "Format Provider" );
        Console.WriteLine( formatter, "-------", "---------------" );

        // Convert the values with and without a format provider.
        Console.WriteLine( formatter, Convert.ToString( ByteA ), 
            Convert.ToString( ByteA, provider ) );
        Console.WriteLine( formatter, Convert.ToString( SByteA ), 
            Convert.ToString( SByteA, provider ) );
        Console.WriteLine( formatter, Convert.ToString( UInt16A ), 
            Convert.ToString( UInt16A, provider ) );
        Console.WriteLine( formatter, Convert.ToString( Int16A ), 
            Convert.ToString( Int16A, provider ) );

        Console.WriteLine( formatter, Convert.ToString( UInt32A ), 
            Convert.ToString( UInt32A, provider ) );
        Console.WriteLine( formatter, Convert.ToString( Int32A ), 
            Convert.ToString( Int32A, provider ) );
        Console.WriteLine( formatter, Convert.ToString( UInt64A ), 
            Convert.ToString( UInt64A, provider ) );
        Console.WriteLine( formatter, Convert.ToString( Int64A ), 
            Convert.ToString( Int64A, provider ) );

        Console.WriteLine( formatter, Convert.ToString( SingleA ), 
            Convert.ToString( SingleA, provider ) );
        Console.WriteLine( formatter, Convert.ToString( DoubleA ), 
            Convert.ToString( DoubleA, provider ) );
        Console.WriteLine( formatter, Convert.ToString( DecimA ), 
            Convert.ToString( DecimA, provider ) );
        Console.WriteLine( formatter, Convert.ToString( ObjDouble ), 
            Convert.ToString( ObjDouble, provider ) );
    }
}

/*
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
*/ 

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

class ConvertNumericProviderDemo
{   
    public static void main(String[] args)
    {    
        // 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.set_NegativeSign("minus ");
        provider.set_NumberDecimalSeparator(" point ");
      
        // These properties will not be applied.
        provider.set_NumberDecimalDigits(2);
        provider.set_NumberGroupSeparator(".");
        provider.set_NumberGroupSizes(new int[]{3});
      
        // Convert these values using default values and the
        // format provider created above.
        ubyte byteA = 140;
        SByte sByteA = (SByte)(-60);
        UInt16 uInt16A = (UInt16)61680;
        short int16A = -3855;
      
        UInt32 uInt32A = (UInt32)(4042322160L);
        int int32A = -252645135;
        UInt64  uInt64A     =(UInt64)8138269444283625712L;
        long    int64A      = -1085102592571150095L;
      
        float singleA = (float)-32.375;
        double doubleA = 61680.3855;
        System.Decimal decimA = System.Convert.ToDecimal(4042322160.252645135);
        Object objDouble = (System.Double)(-98765.4321);
      
        Console.WriteLine(("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."));
        Console.WriteLine(("\nNote: Of the several NumberFormatInfo "
            + "properties that are changed, \nonly the negative sign " 
            + "and decimal separator affect the conversions.\n"));
        Console.WriteLine(formatter, "Default", "Format Provider");
        Console.WriteLine(formatter, "-------", "---------------");
      
        // Convert the values with and without a format provider.
        Console.WriteLine(formatter, Convert.ToString(byteA), 
            Convert.ToString(byteA, provider));
        Console.WriteLine(formatter, Convert.ToString(sByteA), 
            Convert.ToString(sByteA, provider));
        Console.WriteLine(formatter, Convert.ToString(uInt16A), 
            Convert.ToString(uInt16A, provider));
        Console.WriteLine(formatter, Convert.ToString(int16A), 
            Convert.ToString(int16A, provider));
      
        Console.WriteLine(formatter, Convert.ToString(uInt32A), 
            Convert.ToString(uInt32A, provider));
        Console.WriteLine(formatter, Convert.ToString(int32A), 
            Convert.ToString(int32A, provider));
        Console.WriteLine(formatter, Convert.ToString(uInt64A), 
            Convert.ToString(uInt64A, provider));
        Console.WriteLine(formatter, Convert.ToString(int64A), 
            Convert.ToString(int64A, provider));
      
        Console.WriteLine(formatter, Convert.ToString(singleA), 
            Convert.ToString(singleA, provider));
        Console.WriteLine(formatter, Convert.ToString(doubleA), 
            Convert.ToString(doubleA, provider));
        Console.WriteLine(formatter, Convert.ToString(decimA), 
            Convert.ToString(decimA, provider));
        Console.WriteLine(formatter, Convert.ToString(objDouble), 
            Convert.ToString(objDouble, provider));
    } //main
} //ConvertNumericProviderDemo

/*
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 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Édition Media Center, Windows XP Professionnel Édition x64, Windows XP SP2, Windows XP Starter Edition

Le .NET Framework ne prend pas en charge toutes les versions de chaque plate-forme. Pour obtenir la liste des versions prises en charge, consultez Configuration requise.

.NET Framework

Prise en charge dans : 2.0, 1.1, 1.0

.NET Compact Framework

Prise en charge dans : 2.0

Ajouts de la communauté

AJOUTER
Afficher:
© 2014 Microsoft