Convert.ToString Méthode
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Convertit la valeur spécifiée en sa représentation sous forme de chaîne équivalente.
Surcharges
| Nom | Description |
|---|---|
| ToString(Int32, IFormatProvider) |
Convertit la valeur de l’entier signé 32 bits spécifié en sa représentation sous forme de chaîne équivalente, à l’aide des informations de mise en forme spécifiques à la culture spécifiées. |
| ToString(Int16, Int32) |
Convertit la valeur d’un entier signé 16 bits en sa représentation sous forme de chaîne équivalente dans une base spécifiée. |
| ToString(DateTime, IFormatProvider) |
Convertit la valeur de l’élément spécifié DateTime en sa représentation sous forme de chaîne équivalente, à l’aide des informations de mise en forme spécifiques à la culture spécifiées. |
| ToString(Double, IFormatProvider) |
Convertit la valeur du nombre à virgule flottante double précision spécifié en sa représentation sous forme de chaîne équivalente. |
| ToString(Decimal, IFormatProvider) |
Convertit la valeur du nombre décimal spécifié en sa représentation sous forme de chaîne équivalente, à l’aide des informations de mise en forme spécifiques à la culture spécifiées. |
| ToString(Int32, Int32) |
Convertit la valeur d’un entier signé 32 bits en sa représentation sous forme de chaîne équivalente dans une base spécifiée. |
| ToString(Int16, IFormatProvider) |
Convertit la valeur de l’entier signé 16 bits spécifié en sa représentation sous forme de chaîne équivalente, à l’aide des informations de mise en forme spécifiques à la culture spécifiées. |
| ToString(Int64, IFormatProvider) |
Convertit la valeur de l’entier signé 64 bits spécifié en sa représentation sous forme de chaîne équivalente, à l’aide des informations de mise en forme spécifiques à la culture spécifiées. |
| ToString(Single, IFormatProvider) |
Convertit la valeur du nombre à virgule flottante simple précision spécifié en sa représentation sous forme de chaîne équivalente, à l’aide des informations de mise en forme spécifiques à la culture spécifiées. |
| ToString(Object, IFormatProvider) |
Convertit la valeur de l’objet spécifié en sa représentation sous forme de chaîne équivalente à l’aide des informations de mise en forme spécifiques à la culture spécifiées. |
| ToString(SByte, IFormatProvider) |
Convertit la valeur de l’entier signé 8 bits spécifié en sa représentation sous forme de chaîne équivalente, à l’aide des informations de mise en forme spécifiques à la culture spécifiées. |
| ToString(String, IFormatProvider) |
Retourne l’instance de chaîne spécifiée ; aucune conversion réelle n’est effectuée. |
| ToString(UInt16, IFormatProvider) |
Convertit la valeur de l’entier non signé 16 bits spécifié en sa représentation sous forme de chaîne équivalente, à l’aide des informations de mise en forme spécifiques à la culture spécifiées. |
| ToString(UInt32, IFormatProvider) |
Convertit la valeur de l’entier non signé 32 bits spécifié en sa représentation sous forme de chaîne équivalente, à l’aide des informations de mise en forme spécifiques à la culture spécifiées. |
| ToString(UInt64, IFormatProvider) |
Convertit la valeur de l’entier non signé 64 bits spécifié en sa représentation sous forme de chaîne équivalente, à l’aide des informations de mise en forme spécifiques à la culture spécifiées. |
| ToString(Char, IFormatProvider) |
Convertit la valeur du caractère Unicode spécifié en sa représentation sous forme de chaîne équivalente, à l’aide des informations de mise en forme spécifiques à la culture spécifiées. |
| ToString(Int64, Int32) |
Convertit la valeur d’un entier signé 64 bits en sa représentation sous forme de chaîne équivalente dans une base spécifiée. |
| ToString(Byte, Int32) |
Convertit la valeur d’un entier non signé 8 bits en sa représentation sous forme de chaîne équivalente dans une base spécifiée. |
| ToString(Byte, IFormatProvider) |
Convertit la valeur de l’entier non signé 8 bits spécifié en sa représentation sous forme de chaîne équivalente, à l’aide des informations de mise en forme spécifiques à la culture spécifiées. |
| ToString(Boolean, IFormatProvider) |
Convertit la valeur booléenne spécifiée en sa représentation sous forme de chaîne équivalente. |
| ToString(Boolean) |
Convertit la valeur booléenne spécifiée en sa représentation sous forme de chaîne équivalente. |
| ToString(Byte) |
Convertit la valeur de l’entier non signé 8 bits spécifié en sa représentation sous forme de chaîne équivalente. |
| ToString(Char) |
Convertit la valeur du caractère Unicode spécifié en sa représentation sous forme de chaîne équivalente. |
| ToString(DateTime) |
Convertit la valeur de l’élément spécifié DateTime en sa représentation sous forme de chaîne équivalente. |
| ToString(Double) |
Convertit la valeur du nombre à virgule flottante double précision spécifié en sa représentation sous forme de chaîne équivalente. |
| ToString(Int16) |
Convertit la valeur de l’entier signé 16 bits spécifié en sa représentation sous forme de chaîne équivalente. |
| ToString(Int32) |
Convertit la valeur de l’entier signé 32 bits spécifié en sa représentation sous forme de chaîne équivalente. |
| ToString(Decimal) |
Convertit la valeur du nombre décimal spécifié en sa représentation sous forme de chaîne équivalente. |
| ToString(Object) |
Convertit la valeur de l’objet spécifié en sa représentation sous forme de chaîne équivalente. |
| ToString(SByte) |
Convertit la valeur de l’entier signé 8 bits spécifié en sa représentation sous forme de chaîne équivalente. |
| ToString(Single) |
Convertit la valeur du nombre à virgule flottante simple précision spécifié en sa représentation sous forme de chaîne équivalente. |
| ToString(String) |
Retourne l’instance de chaîne spécifiée ; aucune conversion réelle n’est effectuée. |
| ToString(UInt16) |
Convertit la valeur de l’entier non signé 16 bits spécifié en sa représentation sous forme de chaîne équivalente. |
| ToString(UInt32) |
Convertit la valeur de l’entier non signé 32 bits spécifié en sa représentation sous forme de chaîne équivalente. |
| ToString(UInt64) |
Convertit la valeur de l’entier non signé 64 bits spécifié en sa représentation sous forme de chaîne équivalente. |
| ToString(Int64) |
Convertit la valeur de l’entier signé 64 bits spécifié en sa représentation sous forme de chaîne équivalente. |
ToString(Int32, IFormatProvider)
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
Convertit la valeur de l’entier signé 32 bits spécifié en sa représentation sous forme de chaîne équivalente, à l’aide des informations de mise en forme spécifiques à la culture spécifiées.
public:
static System::String ^ ToString(int value, IFormatProvider ^ provider);
public static string ToString(int value, IFormatProvider provider);
public static string ToString(int value, IFormatProvider? provider);
static member ToString : int * IFormatProvider -> string
Public Shared Function ToString (value As Integer, provider As IFormatProvider) As String
Paramètres
- value
- Int32
Entier signé 32 bits à convertir.
- provider
- IFormatProvider
Objet qui fournit des informations de mise en forme propres à la culture.
Retours
Représentation sous forme de chaîne de value.
Exemples
L’exemple suivant définit une classe personnalisée NumberFormatInfo qui définit son signe négatif comme chaîne « ~ » et son signe positif comme chaîne « ! ». Il appelle ensuite la ToString(Int32, IFormatProvider) méthode pour convertir chaque élément dans un tableau d’entiers en sa représentation sous forme de chaîne équivalente. La conversion utilise la culture invariante ainsi que l’objet personnalisé NumberFormatInfo .
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));
// The example displays the following output:
// -2147483648 --> ~2147483648
// 2147483647 --> 2147483647
let numbers = [| Int32.MinValue; Int32.MaxValue |]
let nfi = NumberFormatInfo()
nfi.NegativeSign <- "~"
nfi.PositiveSign <- "!"
for number in numbers do
printfn $"{Convert.ToString(number, CultureInfo.InvariantCulture),-12} --> {Convert.ToString(number, nfi),12}"
// The example displays the following output:
// -2147483648 --> ~2147483648
// 2147483647 --> 2147483647
Dim numbers() As Integer = { Int32.MinValue, Int32.MaxValue}
Dim nfi As New System.Globalization.NumberFormatInfo()
nfi.NegativeSign = "~"
nfi.PositiveSign = "!"
For Each number As Integer In numbers
Console.WriteLine("{0,-12} --> {1,12}", _
Convert.ToString(number, System.Globalization.CultureInfo.InvariantCulture), _
Convert.ToString(number, nfi))
Next
' The example displays the following output:
' -2147483648 --> ~2147483648
' 2147483647 --> 2147483647
Remarques
Cette implémentation est identique à Int32.ToString(IFormatProvider).
S’applique à
ToString(Int16, Int32)
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
Convertit la valeur d’un entier signé 16 bits en sa représentation sous forme de chaîne équivalente dans une base spécifiée.
public:
static System::String ^ ToString(short value, int toBase);
public static string ToString(short value, int toBase);
static member ToString : int16 * int -> string
Public Shared Function ToString (value As Short, toBase As Integer) As String
Paramètres
- value
- Int16
Entier signé 16 bits à convertir.
- toBase
- Int32
Base de la valeur de retour, qui doit être 2, 8, 10 ou 16.
Retours
Représentation sous forme de chaîne dans value la base toBase.
Exceptions
toBase n’est pas 2, 8, 10 ou 16.
Exemples
L’exemple suivant convertit chaque élément d’un tableau d’entiers signés 16 bits en représentations de chaînes binaires, octales, décimales et hexadécimales équivalentes.
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));
}
}
// The example displays the following output:
// 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
let bases = [| 2; 8; 10; 16 |]
let numbers = [| Int16.MinValue; -13621s; -18s; 12s; 19142s; Int16.MaxValue |]
for baseValue in bases do
printfn $"Base {baseValue} conversion:"
for number in numbers do
printfn $" {number,-8} --> 0x{Convert.ToString(number, baseValue)}"
// The example displays the following output:
// 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
Dim bases() As Integer = { 2, 8, 10, 16}
Dim numbers() As Short = { Int16.MinValue, -13621, -18, 12, 19142, _
Int16.MaxValue }
For Each base As Integer In bases
Console.WriteLine("Base {0} conversion:", base)
For Each number As Short In numbers
Console.WriteLine(" {0,-8} --> 0x{1}", _
number, Convert.ToString(number, base))
Next
Next
' The example displays the following output:
' 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
let bases = [2; 8; 10; 16]
let numbers = [ Int16.MinValue; -13621s; -18s; 12s; 19142s; Int16.MaxValue ]
for baseValue in bases do
printfn "Base %i conversion:" baseValue
for number in numbers do
printfn " % -8i --> 0x%s" number (Convert.ToString (number, baseValue))
// The example displays the following output:
// 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
Remarques
Si value elle est positive et toBase est 2, 8 ou 16, la chaîne retournée utilise la représentation de sign-and-magnitude. Si value elle est négative et toBase est 2, 8 ou 16, la chaîne retournée utilise la représentation complémentaire de deux. Cela signifie que le bit d’ordre élevé de l’octet de l’ordre élevé (bit 15) est interprété comme le bit de signe. Si la ToString(Int16, Int32) méthode est appelée pour créer une chaîne qui sera convertie ultérieurement en nombre, une méthode correspondante qui suppose qu’une représentation numérique similaire doit être appelée pour effectuer la conversion. Ces méthodes incluent Convert.ToInt16(String, Int32) et Int16.Parse(String, NumberStyles).
S’applique à
ToString(DateTime, IFormatProvider)
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
Convertit la valeur de l’élément spécifié DateTime en sa représentation sous forme de chaîne équivalente, à l’aide des informations de mise en forme spécifiques à la culture spécifiées.
public:
static System::String ^ ToString(DateTime value, IFormatProvider ^ provider);
public static string ToString(DateTime value, IFormatProvider provider);
public static string ToString(DateTime value, IFormatProvider? provider);
static member ToString : DateTime * IFormatProvider -> string
Public Shared Function ToString (value As DateTime, provider As IFormatProvider) As String
Paramètres
- value
- DateTime
Valeur de date et d’heure à convertir.
- provider
- IFormatProvider
Objet qui fournit des informations de mise en forme propres à la culture.
Retours
Représentation sous forme de chaîne de value.
Exemples
L’exemple suivant convertit une DateTime valeur en sa représentation sous forme de chaîne équivalente dans huit cultures différentes.
// 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);
}
// The example displays the following output:
// 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
// Specify the date to be formatted using various cultures.
let tDate = DateTime(2010, 4, 15, 20, 30, 40, 333)
// Specify the cultures.
let cultureNames =
[| "en-US"; "es-AR"; "fr-FR"; "hi-IN";
"ja-JP"; "nl-NL"; "ru-RU"; "ur-PK" |]
printfn $"Converting the date {Convert.ToString(tDate, CultureInfo.InvariantCulture)}: "
for cultureName in cultureNames do
let culture = CultureInfo cultureName
let dateString = Convert.ToString(tDate, culture)
printfn $" {culture.Name}: {dateString,-12}"
// The example displays the following output:
// 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
' Specify the date to be formatted using various cultures.
Dim tDate As New Date(2010, 4, 15, 20, 30, 40, 333)
' Specify the cultures.
Dim cultureNames() As String = { "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))
For Each cultureName As String In CultureNames
Dim culture As New System.Globalization.CultureInfo(cultureName)
Dim dateString As String = Convert.ToString(tDate, culture)
Console.WriteLine(" {0}: {1,-12}", _
culture.Name, dateString)
Next
' The example displays the following output:
' 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
Remarques
Cette implémentation est identique à DateTime.ToString(IFormatProvider).
S’applique à
ToString(Double, IFormatProvider)
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
Convertit la valeur du nombre à virgule flottante double précision spécifié en sa représentation sous forme de chaîne équivalente.
public:
static System::String ^ ToString(double value, IFormatProvider ^ provider);
public static string ToString(double value, IFormatProvider provider);
public static string ToString(double value, IFormatProvider? provider);
static member ToString : double * IFormatProvider -> string
Public Shared Function ToString (value As Double, provider As IFormatProvider) As String
Paramètres
- value
- Double
Nombre à virgule flottante double précision à convertir.
- provider
- IFormatProvider
Objet qui fournit des informations de mise en forme propres à la culture.
Retours
Représentation sous forme de chaîne de value.
Exemples
L’exemple suivant convertit chaque élément d’un tableau de valeurs en sa représentation sous forme de Double chaîne équivalente dans quatre cultures différentes.
// 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();
}
// The example displays the following output:
// -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
// Define an array of numbers to display.
let numbers = [| -1.5345e16; -123.4321; 19092.123; 1.1734231911290e16 |]
// Define the culture names used to display them.
let cultureNames = [| "en-US"; "fr-FR"; "ja-JP"; "ru-RU" |]
for number in numbers do
printfn $"{Convert.ToString(number, CultureInfo.InvariantCulture)}:"
for cultureName in cultureNames do
let culture = CultureInfo cultureName
printfn " {culture.Name}: {Convert.ToString(number, culture),20}"
printfn ""
// The example displays the following output:
// -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
' Define an array of numbers to display.
Dim numbers() As Double = { -1.5345e16, -123.4321, 19092.123, _
1.1734231911290e16 }
' Define the culture names used to display them.
Dim cultureNames() As String = { "en-US", "fr-FR", "ja-JP", "ru-RU" }
For Each number As Double In numbers
Console.WriteLine("{0}:", Convert.ToString(number, _
System.Globalization.CultureInfo.InvariantCulture))
For Each cultureName As String In cultureNames
Dim culture As New System.Globalization.CultureInfo(cultureName)
Console.WriteLine(" {0}: {1,20}", _
culture.Name, Convert.ToString(number, culture))
Next
Console.WriteLine()
Next
' The example displays the following output:
' -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
Remarques
Cette implémentation est identique à Double.ToString(IFormatProvider)
S’applique à
ToString(Decimal, IFormatProvider)
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
Convertit la valeur du nombre décimal spécifié en sa représentation sous forme de chaîne équivalente, à l’aide des informations de mise en forme spécifiques à la culture spécifiées.
public:
static System::String ^ ToString(System::Decimal value, IFormatProvider ^ provider);
public static string ToString(decimal value, IFormatProvider provider);
public static string ToString(decimal value, IFormatProvider? provider);
static member ToString : decimal * IFormatProvider -> string
Public Shared Function ToString (value As Decimal, provider As IFormatProvider) As String
Paramètres
- value
- Decimal
Nombre décimal à convertir.
- provider
- IFormatProvider
Objet qui fournit des informations de mise en forme propres à la culture.
Retours
Représentation sous forme de chaîne de value.
Exemples
L’exemple suivant convertit chaque élément d’un tableau de valeurs en sa représentation sous forme de Decimal chaîne équivalente dans quatre cultures différentes.
// 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();
}
// The example displays the following output:
// 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
// Define an array of numbers to display.
let numbers =
[| 1734231911290.16m; -17394.32921m; 3193.23m; 98012368321.684m |]
// Define the culture names used to display them.
let cultureNames = [| "en-US"; "fr-FR"; "ja-JP"; "ru-RU" |]
for number in numbers do
printfn $"{Convert.ToString(number, CultureInfo.InvariantCulture)}:"
for cultureName in cultureNames do
let culture = CultureInfo cultureName
printfn $" {culture.Name}: {Convert.ToString(number, culture),20}"
printfn ""
// The example displays the following output:
// 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
' Define an array of numbers to display.
Dim numbers() As Decimal = { 1734231911290.16d, -17394.32921d, _
3193.23d, 98012368321.684d }
' Define the culture names used to display them.
Dim cultureNames() As String = { "en-US", "fr-FR", "ja-JP", "ru-RU" }
For Each number As Decimal In numbers
Console.WriteLine("{0}:", Convert.ToString(number, _
System.Globalization.CultureInfo.InvariantCulture))
For Each cultureName As String In cultureNames
Dim culture As New System.Globalization.CultureInfo(cultureName)
Console.WriteLine(" {0}: {1,20}", _
culture.Name, Convert.ToString(number, culture))
Next
Console.WriteLine()
Next
' The example displays the following output:
' 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
Remarques
Cette implémentation est identique à Decimal.ToString(IFormatProvider).
S’applique à
ToString(Int32, Int32)
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
Convertit la valeur d’un entier signé 32 bits en sa représentation sous forme de chaîne équivalente dans une base spécifiée.
public:
static System::String ^ ToString(int value, int toBase);
public static string ToString(int value, int toBase);
static member ToString : int * int -> string
Public Shared Function ToString (value As Integer, toBase As Integer) As String
Paramètres
- value
- Int32
Entier signé 32 bits à convertir.
- toBase
- Int32
Base de la valeur de retour, qui doit être 2, 8, 10 ou 16.
Retours
Représentation sous forme de chaîne dans value la base toBase.
Exceptions
toBase n’est pas 2, 8, 10 ou 16.
Exemples
L’exemple suivant convertit chaque élément d’un tableau entier en sa représentation sous forme de chaîne en binaire, octal, décimal et hexadécimal.
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));
}
}
// The example displays the following output:
// 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
let bases = [| 2; 8; 10; 16 |]
let numbers =
[| Int32.MinValue; -19327543; -13621; -18; 12; 19142; Int32.MaxValue |]
for baseValue in bases do
printfn $"Base {baseValue} conversion:"
for number in numbers do
printfn $" {number,-15} --> 0x{Convert.ToString(number, baseValue)}"
// The example displays the following output:
// 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
Dim bases() As Integer = { 2, 8, 10, 16}
Dim numbers() As Integer = { Int32.MinValue, -19327543, -13621, -18, 12, _
19142, Int32.MaxValue }
For Each base As Integer In bases
Console.WriteLine("Base {0} conversion:", base)
For Each number As Integer In numbers
Console.WriteLine(" {0,-15} --> 0x{1}", _
number, Convert.ToString(number, base))
Next
Next
' The example displays the following output:
' 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
let bases = [2; 8; 10; 16]
let numbers = [ Int32.MinValue; -19327543; -13621; -18; 12; 19142; Int32.MaxValue ]
for baseValue in bases do
printfn "Base %i conversion:" baseValue
for number in numbers do
printfn " % -15i --> 0x%s" number (Convert.ToString (number, baseValue))
// The example displays the following output:
// 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
Remarques
Si value elle est positive et toBase est 2, 8 ou 16, la chaîne retournée utilise la représentation de sign-and-magnitude. Si value elle est négative et toBase est 2, 8 ou 16, la chaîne retournée utilise la représentation complémentaire de deux. Cela signifie que le bit d’ordre élevé de l’octet le plus élevé (bit 31) est interprété comme le bit de signe. Si la ToString(Int32, Int32) méthode est appelée pour créer une chaîne qui sera convertie ultérieurement en nombre, une méthode correspondante qui suppose qu’une représentation numérique similaire doit être appelée pour effectuer la conversion. Ces méthodes incluent Convert.ToInt32(String, Int32) et Int32.Parse(String, NumberStyles).
S’applique à
ToString(Int16, IFormatProvider)
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
Convertit la valeur de l’entier signé 16 bits spécifié en sa représentation sous forme de chaîne équivalente, à l’aide des informations de mise en forme spécifiques à la culture spécifiées.
public:
static System::String ^ ToString(short value, IFormatProvider ^ provider);
public static string ToString(short value, IFormatProvider provider);
public static string ToString(short value, IFormatProvider? provider);
static member ToString : int16 * IFormatProvider -> string
Public Shared Function ToString (value As Short, provider As IFormatProvider) As String
Paramètres
- value
- Int16
Entier signé 16 bits à convertir.
- provider
- IFormatProvider
Objet qui fournit des informations de mise en forme propres à la culture.
Retours
Représentation sous forme de chaîne de value.
Exemples
L’exemple suivant définit une classe personnalisée NumberFormatInfo qui définit son signe négatif comme chaîne « ~ » et son signe positif comme chaîne « ! ». Il appelle ensuite la ToString(Int16, IFormatProvider) méthode pour convertir chaque élément dans un tableau d’entiers 16 bits en sa représentation sous forme de chaîne équivalente. La conversion utilise la culture invariante ainsi que l’objet personnalisé NumberFormatInfo .
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));
// The example displays the following output:
// -32768 --> ~32768
// 32767 --> 32767
let numbers = [| Int16.MinValue; Int16.MaxValue |]
let nfi = NumberFormatInfo()
nfi.NegativeSign <- "~"
nfi.PositiveSign <- "!"
for number in numbers do
printfn $"{Convert.ToString(number, CultureInfo.InvariantCulture),-8} --> {Convert.ToString(number, nfi),8}"
// The example displays the following output:
// -32768 --> ~32768
// 32767 --> 32767
Dim numbers() As Short = { Int16.MinValue, Int16.MaxValue}
Dim nfi As New System.Globalization.NumberFormatInfo()
nfi.NegativeSign = "~"
nfi.PositiveSign = "!"
For Each number As Short In numbers
Console.WriteLine("{0,-8} --> {1,8}", _
Convert.ToString(number, System.Globalization.CultureInfo.InvariantCulture), _
Convert.ToString(number, nfi))
Next
' The example displays the following output:
' -32768 --> ~32768
' 32767 --> 32767
Remarques
Cette implémentation est identique à Int16.ToString(IFormatProvider).
S’applique à
ToString(Int64, IFormatProvider)
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
Convertit la valeur de l’entier signé 64 bits spécifié en sa représentation sous forme de chaîne équivalente, à l’aide des informations de mise en forme spécifiques à la culture spécifiées.
public:
static System::String ^ ToString(long value, IFormatProvider ^ provider);
public static string ToString(long value, IFormatProvider provider);
public static string ToString(long value, IFormatProvider? provider);
static member ToString : int64 * IFormatProvider -> string
Public Shared Function ToString (value As Long, provider As IFormatProvider) As String
Paramètres
- value
- Int64
Entier signé 64 bits à convertir.
- provider
- IFormatProvider
Objet qui fournit des informations de mise en forme propres à la culture.
Retours
Représentation sous forme de chaîne de value.
Exemples
L’exemple suivant définit une classe personnalisée NumberFormatInfo qui définit son signe négatif comme chaîne « ~ » et son signe positif comme chaîne « ! ». Il appelle ensuite la ToString(Int64, IFormatProvider) méthode pour convertir chaque élément d’un tableau entier long en sa représentation sous forme de chaîne équivalente. La conversion utilise la culture invariante ainsi que l’objet personnalisé NumberFormatInfo .
long[] numbers = { ((long) Int32.MinValue) * 2, ((long) Int32.MaxValue) * 2};
System.Globalization.NumberFormatInfo nfi = new System.Globalization.NumberFormatInfo();
nfi.NegativeSign = "~";
nfi.PositiveSign = "!";
foreach (long number in numbers)
Console.WriteLine("{0,-12} --> {1,12}",
Convert.ToString(number, System.Globalization.CultureInfo.InvariantCulture),
Convert.ToString(number, nfi));
// The example displays the following output:
// -4294967296 --> ~4294967296
// 4294967294 --> 4294967294
let numbers = [| (int64 Int32.MinValue) * 2L; (int64 Int32.MaxValue) * 2L |]
let nfi = NumberFormatInfo()
nfi.NegativeSign <- "~"
nfi.PositiveSign <- "!"
for number in numbers do
printfn $"{Convert.ToString(number, CultureInfo.InvariantCulture),-12} --> {Convert.ToString(number, nfi),12}"
// The example displays the following output:
// -4294967296 --> ~4294967296
// 4294967294 --> 4294967294
Dim numbers() As Long = { CLng(Int32.MinValue) * 2, CLng(Int32.MaxValue) * 2 }
Dim nfi As New System.Globalization.NumberFormatInfo()
nfi.NegativeSign = "~"
nfi.PositiveSign = "!"
For Each number As Long In numbers
Console.WriteLine("{0,-12} --> {1,12}", _
Convert.ToString(number, System.Globalization.CultureInfo.InvariantCulture), _
Convert.ToString(number, nfi))
Next
' The example displays the following output:
' -4294967296 --> ~4294967296
' 4294967294 --> 4294967294
Remarques
Cette implémentation est identique à Int64.ToString(IFormatProvider).
S’applique à
ToString(Single, IFormatProvider)
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
Convertit la valeur du nombre à virgule flottante simple précision spécifié en sa représentation sous forme de chaîne équivalente, à l’aide des informations de mise en forme spécifiques à la culture spécifiées.
public:
static System::String ^ ToString(float value, IFormatProvider ^ provider);
public static string ToString(float value, IFormatProvider provider);
public static string ToString(float value, IFormatProvider? provider);
static member ToString : single * IFormatProvider -> string
Public Shared Function ToString (value As Single, provider As IFormatProvider) As String
Paramètres
- value
- Single
Nombre à virgule flottante simple précision à convertir.
- provider
- IFormatProvider
Objet qui fournit des informations de mise en forme propres à la culture.
Retours
Représentation sous forme de chaîne de value.
Exemples
L’exemple suivant convertit chaque élément d’un tableau de valeurs en sa représentation sous forme de Single chaîne équivalente dans quatre cultures différentes.
// 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();
}
// The example displays the following output:
// -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
// Define an array of numbers to display.
let numbers = [| -1.5345e16f; -123.4321f; 19092.123f; 1.1734231911290e16f |]
// Define the culture names used to display them.
let cultureNames = [| "en-US"; "fr-FR"; "ja-JP"; "ru-RU" |]
for number in numbers do
printfn $"{Convert.ToString(number, CultureInfo.InvariantCulture)}:"
for cultureName in cultureNames do
let culture = CultureInfo cultureName
printfn $" {culture.Name}: {Convert.ToString(number, culture),20}"
printfn ""
// The example displays the following output:
// -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
' Define an array of numbers to display.
Dim numbers() As Single = { -1.5345e16, -123.4321, 19092.123, _
1.1734231911290e16 }
' Define the culture names used to display them.
Dim cultureNames() As String = { "en-US", "fr-FR", "ja-JP", "ru-RU" }
For Each number As Single In numbers
Console.WriteLine("{0}:", Convert.ToString(number, _
System.Globalization.CultureInfo.InvariantCulture))
For Each cultureName As String In cultureNames
Dim culture As New System.Globalization.CultureInfo(cultureName)
Console.WriteLine(" {0}: {1,20}", _
culture.Name, Convert.ToString(number, culture))
Next
Console.WriteLine()
Next
' The example displays the following output:
' -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
Remarques
Cette implémentation est identique à Single.ToString(IFormatProvider).
S’applique à
ToString(Object, IFormatProvider)
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
Convertit la valeur de l’objet spécifié en sa représentation sous forme de chaîne équivalente à l’aide des informations de mise en forme spécifiques à la culture spécifiées.
public:
static System::String ^ ToString(System::Object ^ value, IFormatProvider ^ provider);
public static string ToString(object value, IFormatProvider provider);
public static string? ToString(object? value, IFormatProvider? provider);
static member ToString : obj * IFormatProvider -> string
Public Shared Function ToString (value As Object, provider As IFormatProvider) As String
Paramètres
- value
- Object
Objet qui fournit la valeur à convertir, ou null.
- provider
- IFormatProvider
Objet qui fournit des informations de mise en forme propres à la culture.
Retours
Représentation sous forme de chaîne d’un valueEmptyvalue objet dont la valeur est null. Si value c’est nullle cas, la méthode retourne null.
Exemples
L’exemple suivant définit une Temperature classe qui remplace la Object.ToString méthode, mais n’implémente pas l’interface IConvertible . L’exemple montre comment appeler la Convert.ToString(Object, IFormatProvider) méthode, à son tour, appeler la Temperature.ToString méthode.
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));
}
}
// The example dosplays the following output:
// -40.00 °C
// 0.00 °C
// 100.00 °C
open System
type Temperature(temperature: decimal) =
member _.Celsius =
temperature
member _.Kelvin =
temperature + 273.15m
member _.Fahrenheit =
Math.Round(temperature * 9m / 5m + 32m |> decimal, 2)
override _.ToString() =
temperature.ToString("N2") + " °C"
let cold = Temperature -40
let freezing = Temperature 0
let boiling = Temperature 100
printfn $"{Convert.ToString(cold, null)}"
printfn $"{Convert.ToString(freezing, null)}"
printfn $"{Convert.ToString(boiling, null)}"
// The example dosplays the following output:
// -40.00 °C
// 0.00 °C
// 100.00 °C
Public Class Temperature
Private m_Temp As Decimal
Public Sub New(temperature As Decimal)
Me.m_Temp = temperature
End Sub
Public ReadOnly Property Celsius() As Decimal
Get
Return Me.m_Temp
End Get
End Property
Public ReadOnly Property Kelvin() As Decimal
Get
Return Me.m_Temp + 273.15d
End Get
End Property
Public ReadOnly Property Fahrenheit() As Decimal
Get
Return Math.Round(CDec(Me.m_Temp * 9 / 5 + 32), 2)
End Get
End Property
Public Overrides Function ToString() As String
Return m_Temp.ToString("N2") & " °C"
End Function
End Class
Module Example
Public Sub Main()
Dim cold As New Temperature(-40)
Dim freezing As New Temperature(0)
Dim boiling As New Temperature(100)
Console.WriteLine(Convert.ToString(cold, Nothing))
Console.WriteLine(Convert.ToString(freezing, Nothing))
Console.WriteLine(Convert.ToString(boiling, Nothing))
End Sub
End Module
' The example displays the following output:
' -40.00 °C
' 0.00 °C
' 100.00 °C
L’exemple suivant définit une Temperature classe qui implémente l’interface IFormattable , mais qui n’implémente pas l’interface IConvertible . Son IFormattable.ToString implémentation représente la Temperature valeur en Celsius, Fahrenheit ou Kelvin, en fonction de la chaîne de format. L’exemple définit également une TemperatureProvider classe qui implémente IFormatProvider et fournit une chaîne de format générée de manière aléatoire utilisée par l’implémentation IFormattable de la Temperature classe.
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));
}
}
// The example displays output like the following:
// -40.00 °C
// 273.15 K
// 100.00 °C
open System
[<AllowNullLiteral>]
type TemperatureProvider() =
let fmtStrings = [| "C"; "G"; "F"; "K" |]
let rnd = Random()
member _.Format =
fmtStrings[rnd.Next(0, fmtStrings.Length)]
interface IFormatProvider with
member this.GetFormat(formatType: Type) =
this
type Temperature(temperature: decimal) =
member _.Celsius =
temperature
member _.Kelvin =
temperature + 273.15m
member _.Fahrenheit =
Math.Round(temperature * 9m / 5m + 32m, 2)
override this.ToString() =
this.ToString("G", null)
member this.ToString(fmt: string, provider: IFormatProvider) =
let formatter =
match provider with
| null -> null
| _ ->
match provider.GetFormat typeof<TemperatureProvider> with
| :? TemperatureProvider as x -> x
| _ -> null
let fmt =
if String.IsNullOrWhiteSpace fmt then
if formatter <> null then
formatter.Format
else
"G"
else fmt
match fmt.ToUpper() with
| "G"
| "C" ->
$"{temperature:N2} °C"
| "F" ->
$"{this.Fahrenheit:N2} °F"
| "K" ->
$"{this.Kelvin:N2} K"
| _ ->
raise (FormatException $"'{fmt}' is not a valid format specifier.")
let cold = Temperature -40
let freezing = Temperature 0
let boiling = Temperature 100
let tp = TemperatureProvider()
printfn $"{Convert.ToString(cold, tp)}"
printfn $"{Convert.ToString(freezing, tp)}"
printfn $"{Convert.ToString(boiling, tp)}"
// The example displays output like the following:
// -40.00 °C
// 273.15 K
// 100.00 °C
Public Class Temperature : Implements IFormattable
Private m_Temp As Decimal
Public Sub New(temperature As Decimal)
Me.m_Temp = temperature
End Sub
Public ReadOnly Property Celsius As Decimal
Get
Return Me.m_Temp
End Get
End Property
Public ReadOnly Property Kelvin As Decimal
Get
Return Me.m_Temp + 273.15d
End Get
End Property
Public ReadOnly Property Fahrenheit As Decimal
Get
Return Math.Round(CDec(Me.m_Temp * 9 / 5 + 32), 2)
End Get
End Property
Public Overrides Function ToString() As String
Return ToString("G", Nothing)
End Function
Public Overloads Function ToString(fmt As String,
provider As IFormatProvider) As String _
Implements IFormattable.ToString
Dim formatter As TemperatureProvider = Nothing
If provider IsNot Nothing Then formatter = TryCast(provider.GetFormat(GetType(TemperatureProvider)),
TemperatureProvider)
If String.IsNullOrWhiteSpace(fmt) Then
If formatter IsNot Nothing Then
fmt = formatter.Format
Else
fmt = "G"
End If
End If
Select Case fmt.ToUpper()
Case "G", "C"
Return m_Temp.ToString("N2") & " °C"
Case "F"
Return Fahrenheit.ToString("N2") + " °F"
Case "K"
Return Kelvin.ToString("N2") + " K"
Case Else
Throw New FormatException(String.Format("'{0}' is not a valid format specifier.", fmt))
End Select
End Function
End Class
Public Class TemperatureProvider : Implements IFormatProvider
Private fmtStrings() As String = { "C", "G", "F", "K" }
Private rnd As New Random()
Public Function GetFormat(formatType As Type) As Object _
Implements IFormatProvider.GetFormat
Return Me
End Function
Public ReadOnly Property Format As String
Get
Return fmtStrings(rnd.Next(0, fmtStrings.Length))
End Get
End Property
End Class
Module Example
Public Sub Main()
Dim cold As New Temperature(-40)
Dim freezing As New Temperature(0)
Dim boiling As New Temperature(100)
Dim tp As New TemperatureProvider()
Console.WriteLine(Convert.ToString(cold, tp))
Console.WriteLine(Convert.ToString(freezing, tp))
Console.WriteLine(Convert.ToString(boiling, tp))
End Sub
End Module
' The example displays output like the following:
' -40.00 °C
' 273.15 K
' 100.00 °C
Remarques
Si le value paramètre implémente l’interface IConvertible , la méthode appelle l’implémentation IConvertible.ToString(IFormatProvider) de value. Sinon, si le value paramètre implémente l’interface IFormattable , la méthode appelle son IFormattable.ToString(String, IFormatProvider) implémentation. Si value aucune des deux interfaces n’est implémentée, la méthode appelle la value méthode du ToString() paramètre et le provider paramètre est ignoré.
Le provider paramètre est utilisé si le value paramètre implémente l’interface ou IConvertible l’interfaceIFormattable. L’utilisation la plus courante du provider paramètre consiste à spécifier des informations propres à la culture utilisées dans la conversion de value. Par exemple, si le value paramètre est un nombre décimal négatif, le provider paramètre peut fournir des informations spécifiques à la culture sur la notation utilisée pour le signe négatif et le séparateur décimal. Le deuxième exemple de la section suivante illustre un fournisseur de format qui ne fournit pas d’informations de mise en forme sensibles à la culture.
S’applique à
ToString(SByte, IFormatProvider)
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
Important
Cette API n’est pas conforme CLS.
Convertit la valeur de l’entier signé 8 bits spécifié en sa représentation sous forme de chaîne équivalente, à l’aide des informations de mise en forme spécifiques à la culture spécifiées.
public:
static System::String ^ ToString(System::SByte value, IFormatProvider ^ provider);
[System.CLSCompliant(false)]
public static string ToString(sbyte value, IFormatProvider provider);
[System.CLSCompliant(false)]
public static string ToString(sbyte value, IFormatProvider? provider);
[<System.CLSCompliant(false)>]
static member ToString : sbyte * IFormatProvider -> string
Public Shared Function ToString (value As SByte, provider As IFormatProvider) As String
Paramètres
- value
- SByte
Entier signé 8 bits à convertir.
- provider
- IFormatProvider
Objet qui fournit des informations de mise en forme propres à la culture.
Retours
Représentation sous forme de chaîne de value.
- Attributs
Exemples
L’exemple suivant définit une classe personnalisée NumberFormatInfo qui définit son signe négatif comme chaîne « ~ » et son signe positif comme chaîne « ! ». Il appelle ensuite la Convert.ToString(SByte, IFormatProvider) méthode pour convertir chaque élément dans le tableau d’octets signé en sa représentation sous forme de chaîne équivalente. La conversion utilise la culture invariante ainsi que l’objet personnalisé NumberFormatInfo .
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));
// The example displays the following output:
// ~128
// ~12
// 17
// 127
let numbers = [| SByte.MinValue; -12y; 17y; SByte.MaxValue |]
let nfi = NumberFormatInfo()
nfi.NegativeSign <- "~"
nfi.PositiveSign <- "!"
for number in numbers do
printfn $"{Convert.ToString(number, nfi)}"
// The example displays the following output:
// ~128
// ~12
// 17
// 127
Dim numbers() As SByte = { SByte.MinValue, -12, 17, SByte.MaxValue}
Dim nfi As New System.Globalization.NumberFormatInfo()
nfi.NegativeSign = "~"
nfi.PositiveSign = "!"
For Each number As SByte In numbers
Console.WriteLine(Convert.ToString(number, nfi))
Next
' The example displays the following output:
' ~128
' ~12
' 17
' 127
Remarques
Cette implémentation est identique à SByte.ToString(IFormatProvider).
S’applique à
ToString(String, IFormatProvider)
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
Retourne l’instance de chaîne spécifiée ; aucune conversion réelle n’est effectuée.
public:
static System::String ^ ToString(System::String ^ value, IFormatProvider ^ provider);
public static string? ToString(string? value, IFormatProvider? provider);
public static string ToString(string value, IFormatProvider provider);
static member ToString : string * IFormatProvider -> string
Public Shared Function ToString (value As String, provider As IFormatProvider) As String
Paramètres
- value
- String
Chaîne à retourner.
- provider
- IFormatProvider
Objet qui fournit des informations de mise en forme propres à la culture. Ce paramètre est ignoré.
Retours
value est retourné inchangé.
Exemples
L’exemple suivant appelle la ToString méthode avec un String paramètre. La méthode retourne l’objet non modifié String sans référencer l’objet IFormatProvider .
// 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
*/
// Example of Convert.ToString(non-numeric types, IFormatProvider).
open System
open System.Globalization
// Create an instance of the IFormatProvider with an object expression.
let provider =
{ new IFormatProvider with
// Normally, GetFormat returns an object of the requested type
// (usually itself) if it is able; otherwise, it returns Nothing.
member _.GetFormat(argType: Type) =
// Here, the type of argType is displayed, and GetFormat
// always returns Nothing.
printf $"{argType,-40}"
null
}
// Convert these values using DummyProvider.
let Int32A = -252645135
let DoubleA = 61680.3855
let ObjDouble = -98765.4321 :> obj
let DayTimeA = DateTime(2001, 9, 11, 13, 45, 0)
let BoolA = true
let StringA = "Qwerty"
let CharA = '$'
let TSpanA = TimeSpan(0, 18, 0)
let ObjOther = provider :> obj
[<EntryPoint>]
let main _ =
printfn
"""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."""
// The format provider is called for these conversions.
printfn ""
let converted = Convert.ToString(Int32A, provider)
printfn $"int {converted}"
let converted = Convert.ToString(DoubleA, provider)
printfn $"double {converted}"
let converted = Convert.ToString(ObjDouble, provider)
printfn $"object {converted}"
let converted = Convert.ToString(DayTimeA, provider)
printfn $"DateTime {converted}"
// The format provider is not called for these conversions.
printfn ""
let converted = Convert.ToString(BoolA, provider)
printfn $"bool {converted}"
let converted = Convert.ToString(StringA, provider)
printfn $"string {converted}"
let converted = Convert.ToString(CharA, provider)
printfn $"char {converted}"
let converted = Convert.ToString(TSpanA, provider)
printfn $"TimeSpan {converted}"
let converted = Convert.ToString(ObjOther, provider)
printfn $"object {converted}"
0
// 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
' Example of Convert.ToString( non-numeric types, IFormatProvider ).
Imports System.Globalization
' An instance of this class can be passed to methods that require
' an IFormatProvider.
Public Class DummyProvider
Implements IFormatProvider
' Normally, GetFormat returns an object of the requested type
' (usually itself) if it is able; otherwise, it returns Nothing.
Public Function GetFormat( argType As Type ) As Object _
Implements IFormatProvider.GetFormat
' Here, the type of argType is displayed, and GetFormat
' always returns Nothing.
Console.Write( "{0,-40}", argType.ToString( ) )
Return Nothing
End Function
End Class
Module ConvertNonNumericProviderDemo
Sub Main( )
' Create an instance of the IFormatProvider.
Dim provider As New DummyProvider( )
Dim converted As String
' Convert these values using DummyProvider.
Dim Int32A As Integer = -252645135
Dim DoubleA As Double = 61680.3855
Dim ObjDouble As Object = CType( -98765.4321, Object )
Dim DayTimeA As DateTime = _
new DateTime( 2001, 9, 11, 13, 45, 0 )
Dim BoolA As Boolean = True
Dim StringA As String = "Qwerty"
Dim CharA As Char = "$"c
Dim TSpanA As TimeSpan = New TimeSpan( 0, 18, 0 )
Dim ObjOther As Object = CType( provider, Object )
Console.WriteLine( "This example of " & _
"Convert.ToString( non-numeric, IFormatProvider ) " & _
vbCrLf & "generates the following output. The " & _
"provider type, argument type, " & vbCrLf & "and " & _
"argument value are displayed." )
Console.WriteLine( vbCrLf & _
"Note: The IFormatProvider object is not called for " & _
"Boolean, String, " & vbCrLf & "Char, TimeSpan, " & _
"and non-numeric Object." )
' The format provider is called for these conversions.
Console.WriteLine( )
converted = Convert.ToString( Int32A, provider )
Console.WriteLine( "Int32 {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( "Boolean {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 )
End Sub
End Module
' 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 Int32 -252645135
' System.Globalization.NumberFormatInfo Double 61680.3855
' System.Globalization.NumberFormatInfo Object -98765.4321
' System.Globalization.DateTimeFormatInfo DateTime 9/11/2001 1:45:00 PM
'
' Boolean True
' String Qwerty
' Char $
' TimeSpan 00:18:00
' Object DummyProvider
S’applique à
ToString(UInt16, IFormatProvider)
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
Important
Cette API n’est pas conforme CLS.
Convertit la valeur de l’entier non signé 16 bits spécifié en sa représentation sous forme de chaîne équivalente, à l’aide des informations de mise en forme spécifiques à la culture spécifiées.
public:
static System::String ^ ToString(System::UInt16 value, IFormatProvider ^ provider);
[System.CLSCompliant(false)]
public static string ToString(ushort value, IFormatProvider provider);
[System.CLSCompliant(false)]
public static string ToString(ushort value, IFormatProvider? provider);
[<System.CLSCompliant(false)>]
static member ToString : uint16 * IFormatProvider -> string
Public Shared Function ToString (value As UShort, provider As IFormatProvider) As String
Paramètres
- value
- UInt16
Entier non signé 16 bits à convertir.
- provider
- IFormatProvider
Objet qui fournit des informations de mise en forme propres à la culture.
Retours
Représentation sous forme de chaîne de value.
- Attributs
Exemples
L’exemple suivant définit une classe personnalisée NumberFormatInfo qui définit son signe négatif comme chaîne « ~ » et son signe positif comme chaîne « ! ». Il appelle ensuite la ToString(UInt16, IFormatProvider) méthode pour convertir une valeur entière non signée 16 bits en sa représentation sous forme de chaîne équivalente. La conversion utilise à la fois la culture invariante et l’objet personnalisé NumberFormatInfo . La sortie indique que ces informations de mise en forme ne sont pas utilisées, car par défaut, le spécificateur de format « G » n’inclut pas de signe positif avec des valeurs positives.
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));
// The example displays the following output:
// 65535 --> 65535
let number = UInt16.MaxValue
let nfi = NumberFormatInfo()
nfi.NegativeSign <- "~"
nfi.PositiveSign <- "!"
printfn $"{Convert.ToString(number, CultureInfo.InvariantCulture),-6} --> {Convert.ToString(number, nfi),6}"
// The example displays the following output:
// 65535 --> 65535
Dim number As UShort = UInt16.MaxValue
Dim nfi As 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))
' The example displays the following output:
' 65535 --> 65535
Remarques
Cette implémentation est identique à UInt16.ToString(IFormatProvider).
S’applique à
ToString(UInt32, IFormatProvider)
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
Important
Cette API n’est pas conforme CLS.
Convertit la valeur de l’entier non signé 32 bits spécifié en sa représentation sous forme de chaîne équivalente, à l’aide des informations de mise en forme spécifiques à la culture spécifiées.
public:
static System::String ^ ToString(System::UInt32 value, IFormatProvider ^ provider);
[System.CLSCompliant(false)]
public static string ToString(uint value, IFormatProvider provider);
[System.CLSCompliant(false)]
public static string ToString(uint value, IFormatProvider? provider);
[<System.CLSCompliant(false)>]
static member ToString : uint32 * IFormatProvider -> string
Public Shared Function ToString (value As UInteger, provider As IFormatProvider) As String
Paramètres
- value
- UInt32
Entier non signé 32 bits à convertir.
- provider
- IFormatProvider
Objet qui fournit des informations de mise en forme propres à la culture.
Retours
Représentation sous forme de chaîne de value.
- Attributs
Exemples
L’exemple suivant définit une classe personnalisée NumberFormatInfo qui définit son signe négatif comme chaîne « ~ » et son signe positif comme chaîne « ! ». Il appelle ensuite la ToString(UInt32, IFormatProvider) méthode pour convertir une valeur entière non signée en sa représentation sous forme de chaîne équivalente. La conversion utilise à la fois la culture invariante et l’objet personnalisé NumberFormatInfo . La sortie indique que ces informations de mise en forme ne sont pas utilisées, car par défaut, le spécificateur de format « G » n’inclut pas de signe positif avec des valeurs positives.
uint number = UInt32.MaxValue;
System.Globalization.NumberFormatInfo nfi = new System.Globalization.NumberFormatInfo();
nfi.NegativeSign = "~";
nfi.PositiveSign = "!";
Console.WriteLine("{0,-8} --> {1,8}",
Convert.ToString(number, System.Globalization.CultureInfo.InvariantCulture),
Convert.ToString(number, nfi));
// The example displays the following output:
// 4294967295 --> 4294967295
let number = UInt32.MaxValue
let nfi = NumberFormatInfo()
nfi.NegativeSign <- "~"
nfi.PositiveSign <- "!"
printfn $"{Convert.ToString(number, CultureInfo.InvariantCulture),-8} --> {Convert.ToString(number, nfi),8}"
// The example displays the following output:
// 4294967295 --> 4294967295
Dim number As UInteger = UInt32.MaxValue
Dim nfi As New System.Globalization.NumberFormatInfo()
nfi.NegativeSign = "~"
nfi.PositiveSign = "!"
Console.WriteLine("{0,-8} --> {1,8}", _
Convert.ToString(number, System.Globalization.CultureInfo.InvariantCulture), _
Convert.ToString(number, nfi))
' The example displays the following output:
' 4294967295 --> 4294967295
Remarques
Cette implémentation est identique à UInt32.ToString(IFormatProvider).
S’applique à
ToString(UInt64, IFormatProvider)
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
Important
Cette API n’est pas conforme CLS.
Convertit la valeur de l’entier non signé 64 bits spécifié en sa représentation sous forme de chaîne équivalente, à l’aide des informations de mise en forme spécifiques à la culture spécifiées.
public:
static System::String ^ ToString(System::UInt64 value, IFormatProvider ^ provider);
[System.CLSCompliant(false)]
public static string ToString(ulong value, IFormatProvider provider);
[System.CLSCompliant(false)]
public static string ToString(ulong value, IFormatProvider? provider);
[<System.CLSCompliant(false)>]
static member ToString : uint64 * IFormatProvider -> string
Public Shared Function ToString (value As ULong, provider As IFormatProvider) As String
Paramètres
- value
- UInt64
Entier non signé 64 bits à convertir.
- provider
- IFormatProvider
Objet qui fournit des informations de mise en forme propres à la culture.
Retours
Représentation sous forme de chaîne de value.
- Attributs
Exemples
L’exemple suivant définit une classe personnalisée NumberFormatInfo qui définit son signe négatif comme chaîne « ~ » et son signe positif comme chaîne « ! ». Il appelle ensuite la ToString(UInt64, IFormatProvider) méthode pour convertir une valeur entière longue non signée en sa représentation sous forme de chaîne équivalente. La conversion utilise à la fois la culture invariante et l’objet personnalisé NumberFormatInfo . La sortie indique que ces informations de mise en forme ne sont pas utilisées, car par défaut, le spécificateur de format « G » n’inclut pas de signe positif avec des valeurs positives.
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));
// The example displays the following output:
// 18446744073709551615 --> 18446744073709551615
let number = UInt64.MaxValue
let nfi = NumberFormatInfo()
nfi.NegativeSign <- "~"
nfi.PositiveSign <- "!"
printfn $"{Convert.ToString(number, CultureInfo.InvariantCulture),-12} --> {Convert.ToString(number, nfi),12}"
// The example displays the following output:
// 18446744073709551615 --> 18446744073709551615
Dim number As ULong = UInt64.MaxValue
Dim nfi As 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))
' The example displays the following output:
' 18446744073709551615 --> 18446744073709551615
Remarques
Cette implémentation est identique à UInt64.ToString(IFormatProvider).
S’applique à
ToString(Char, IFormatProvider)
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
Convertit la valeur du caractère Unicode spécifié en sa représentation sous forme de chaîne équivalente, à l’aide des informations de mise en forme spécifiques à la culture spécifiées.
public:
static System::String ^ ToString(char value, IFormatProvider ^ provider);
public static string ToString(char value, IFormatProvider provider);
public static string ToString(char value, IFormatProvider? provider);
static member ToString : char * IFormatProvider -> string
Public Shared Function ToString (value As Char, provider As IFormatProvider) As String
Paramètres
- value
- Char
Caractère Unicode à convertir.
- provider
- IFormatProvider
Objet qui fournit des informations de mise en forme propres à la culture. Ce paramètre est ignoré.
Retours
Représentation sous forme de chaîne de value.
Exemples
L’exemple suivant convertit une Char valeur en une String avec la méthode, à l’aide ToString d’un IFormatProvider objet qui affiche le type du fournisseur de format pour lequel il est appelé. L’exemple montre que l’objet IFormatProvider n’est pas référencé.
// 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
*/
// Example of Convert.ToString(non-numeric types, IFormatProvider).
open System
open System.Globalization
// Create an instance of the IFormatProvider with an object expression.
let provider =
{ new IFormatProvider with
// Normally, GetFormat returns an object of the requested type
// (usually itself) if it is able; otherwise, it returns Nothing.
member _.GetFormat(argType: Type) =
// Here, the type of argType is displayed, and GetFormat
// always returns Nothing.
printf $"{argType,-40}"
null
}
// Convert these values using DummyProvider.
let Int32A = -252645135
let DoubleA = 61680.3855
let ObjDouble = -98765.4321 :> obj
let DayTimeA = DateTime(2001, 9, 11, 13, 45, 0)
let BoolA = true
let StringA = "Qwerty"
let CharA = '$'
let TSpanA = TimeSpan(0, 18, 0)
let ObjOther = provider :> obj
[<EntryPoint>]
let main _ =
printfn
"""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."""
// The format provider is called for these conversions.
printfn ""
let converted = Convert.ToString(Int32A, provider)
printfn $"int {converted}"
let converted = Convert.ToString(DoubleA, provider)
printfn $"double {converted}"
let converted = Convert.ToString(ObjDouble, provider)
printfn $"object {converted}"
let converted = Convert.ToString(DayTimeA, provider)
printfn $"DateTime {converted}"
// The format provider is not called for these conversions.
printfn ""
let converted = Convert.ToString(BoolA, provider)
printfn $"bool {converted}"
let converted = Convert.ToString(StringA, provider)
printfn $"string {converted}"
let converted = Convert.ToString(CharA, provider)
printfn $"char {converted}"
let converted = Convert.ToString(TSpanA, provider)
printfn $"TimeSpan {converted}"
let converted = Convert.ToString(ObjOther, provider)
printfn $"object {converted}"
0
// 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
' Example of Convert.ToString( non-numeric types, IFormatProvider ).
Imports System.Globalization
' An instance of this class can be passed to methods that require
' an IFormatProvider.
Public Class DummyProvider
Implements IFormatProvider
' Normally, GetFormat returns an object of the requested type
' (usually itself) if it is able; otherwise, it returns Nothing.
Public Function GetFormat( argType As Type ) As Object _
Implements IFormatProvider.GetFormat
' Here, the type of argType is displayed, and GetFormat
' always returns Nothing.
Console.Write( "{0,-40}", argType.ToString( ) )
Return Nothing
End Function
End Class
Module ConvertNonNumericProviderDemo
Sub Main( )
' Create an instance of the IFormatProvider.
Dim provider As New DummyProvider( )
Dim converted As String
' Convert these values using DummyProvider.
Dim Int32A As Integer = -252645135
Dim DoubleA As Double = 61680.3855
Dim ObjDouble As Object = CType( -98765.4321, Object )
Dim DayTimeA As DateTime = _
new DateTime( 2001, 9, 11, 13, 45, 0 )
Dim BoolA As Boolean = True
Dim StringA As String = "Qwerty"
Dim CharA As Char = "$"c
Dim TSpanA As TimeSpan = New TimeSpan( 0, 18, 0 )
Dim ObjOther As Object = CType( provider, Object )
Console.WriteLine( "This example of " & _
"Convert.ToString( non-numeric, IFormatProvider ) " & _
vbCrLf & "generates the following output. The " & _
"provider type, argument type, " & vbCrLf & "and " & _
"argument value are displayed." )
Console.WriteLine( vbCrLf & _
"Note: The IFormatProvider object is not called for " & _
"Boolean, String, " & vbCrLf & "Char, TimeSpan, " & _
"and non-numeric Object." )
' The format provider is called for these conversions.
Console.WriteLine( )
converted = Convert.ToString( Int32A, provider )
Console.WriteLine( "Int32 {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( "Boolean {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 )
End Sub
End Module
' 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 Int32 -252645135
' System.Globalization.NumberFormatInfo Double 61680.3855
' System.Globalization.NumberFormatInfo Object -98765.4321
' System.Globalization.DateTimeFormatInfo DateTime 9/11/2001 1:45:00 PM
'
' Boolean True
' String Qwerty
' Char $
' TimeSpan 00:18:00
' Object DummyProvider
Remarques
Cette implémentation est identique à Char.ToString(). Le paramètre provider est ignoré.
S’applique à
ToString(Int64, Int32)
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
Convertit la valeur d’un entier signé 64 bits en sa représentation sous forme de chaîne équivalente dans une base spécifiée.
public:
static System::String ^ ToString(long value, int toBase);
public static string ToString(long value, int toBase);
static member ToString : int64 * int -> string
Public Shared Function ToString (value As Long, toBase As Integer) As String
Paramètres
- value
- Int64
Entier signé 64 bits à convertir.
- toBase
- Int32
Base de la valeur de retour, qui doit être 2, 8, 10 ou 16.
Retours
Représentation sous forme de chaîne dans value la base toBase.
Exceptions
toBase n’est pas 2, 8, 10 ou 16.
Exemples
L’exemple suivant convertit chaque élément d’un tableau entier long en représentations de chaînes binaires, hexadécimales, décimales et hexadécimales équivalentes.
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));
}
}
// The example displays the following output:
// 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
let bases = [| 2; 8; 10; 16 |]
let numbers =
[| Int64.MinValue; -193275430; -13621; -18; 12; 1914206117; Int64.MaxValue |]
for baseValue in bases do
printfn $"Base {baseValue} conversion:"
for number in numbers do
printfn $" {number,-23} --> 0x{Convert.ToString(number, baseValue)}"
// The example displays the following output:
// 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
Dim bases() As Integer = { 2, 8, 10, 16}
Dim numbers() As Long = { Int64.MinValue, -193275430, -13621, -18, 12, _
1914206117, Int64.MaxValue }
For Each base As Integer In bases
Console.WriteLine("Base {0} conversion:", base)
For Each number As Long In numbers
Console.WriteLine(" {0,-23} --> 0x{1}", _
number, Convert.ToString(number, base))
Next
Next
' The example displays the following output:
' 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
let bases = [2; 8; 10; 16]
let numbers = [ Int64.MinValue; -193275430L; -13621L; -18L; 12L; 1914206117L; Int64.MaxValue ]
for baseValue in bases do
printfn "Base %i conversion:" baseValue
for number in numbers do
printfn " % -23i --> 0x%s" number (Convert.ToString (number, baseValue))
// The example displays the following output:
// 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
Remarques
Si value elle est positive et toBase n’est pas 10, la chaîne retournée utilise la représentation de sign-and-magnitude. Si value elle est négative et toBase n’est pas 10, la chaîne retournée utilise la représentation complémentaire de deux. Cela signifie que le bit d’ordre élevé de l’octet le plus élevé (bit 63) est interprété comme le bit de signe. Si la ToString(Int64, Int32) méthode est appelée pour créer une chaîne qui sera convertie ultérieurement en nombre, une méthode correspondante qui suppose qu’une représentation numérique similaire doit être appelée pour effectuer la conversion. Ces méthodes incluent Convert.ToInt64(String, Int32) et Int64.Parse(String, NumberStyles).
S’applique à
ToString(Byte, Int32)
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
Convertit la valeur d’un entier non signé 8 bits en sa représentation sous forme de chaîne équivalente dans une base spécifiée.
public:
static System::String ^ ToString(System::Byte value, int toBase);
public static string ToString(byte value, int toBase);
static member ToString : byte * int -> string
Public Shared Function ToString (value As Byte, toBase As Integer) As String
Paramètres
- value
- Byte
Entier non signé 8 bits à convertir.
- toBase
- Int32
Base de la valeur de retour, qui doit être 2, 8, 10 ou 16.
Retours
Représentation sous forme de chaîne dans value la base toBase.
Exceptions
toBase n’est pas 2, 8, 10 ou 16.
Exemples
L’exemple suivant convertit chaque élément d’un tableau d’octets en représentations de chaînes binaires, hexadécimales, décimales et hexadécimales équivalentes.
int[] bases = { 2, 8, 10, 16};
byte[] numbers = { Byte.MinValue, 12, 103, Byte.MaxValue};
foreach (int baseValue in bases)
{
Console.WriteLine("Base {0} conversion:", baseValue);
foreach (byte number in numbers)
{
Console.WriteLine(" {0,-5} --> 0x{1}",
number, Convert.ToString(number, baseValue));
}
}
// The example displays the following output:
// Base 2 conversion:
// 0 --> 0x0
// 12 --> 0x1100
// 103 --> 0x1100111
// 255 --> 0x11111111
// Base 8 conversion:
// 0 --> 0x0
// 12 --> 0x14
// 103 --> 0x147
// 255 --> 0x377
// Base 10 conversion:
// 0 --> 0x0
// 12 --> 0x12
// 103 --> 0x103
// 255 --> 0x255
// Base 16 conversion:
// 0 --> 0x0
// 12 --> 0xc
// 103 --> 0x67
// 255 --> 0xff
let bases = [| 2; 8; 10; 16 |]
let numbers = [| Byte.MinValue; 12uy; 103uy; Byte.MaxValue |]
for baseValue in bases do
printfn $"Base {baseValue} conversion:"
for number in numbers do
Console.WriteLine(" {0,-5} --> 0x{1}",
number, Convert.ToString(number, baseValue))
// The example displays the following output:
// Base 2 conversion:
// 0 --> 0x0
// 12 --> 0x1100
// 103 --> 0x1100111
// 255 --> 0x11111111
// Base 8 conversion:
// 0 --> 0x0
// 12 --> 0x14
// 103 --> 0x147
// 255 --> 0x377
// Base 10 conversion:
// 0 --> 0x0
// 12 --> 0x12
// 103 --> 0x103
// 255 --> 0x255
// Base 16 conversion:
// 0 --> 0x0
// 12 --> 0xc
// 103 --> 0x67
// 255 --> 0xff
Dim bases() As Integer = { 2, 8, 10, 16}
Dim numbers() As Byte = { Byte.MinValue, 12, 103, Byte.MaxValue}
For Each base As Integer In bases
Console.WriteLine("Base {0} conversion:", base)
For Each number As Byte In numbers
Console.WriteLine(" {0,-5} --> 0x{1}", _
number, Convert.ToString(number, base))
Next
Next
' The example displays the following output:
' Base 2 conversion:
' 0 --> 0x0
' 12 --> 0x1100
' 103 --> 0x1100111
' 255 --> 0x11111111
' Base 8 conversion:
' 0 --> 0x0
' 12 --> 0x14
' 103 --> 0x147
' 255 --> 0x377
' Base 10 conversion:
' 0 --> 0x0
' 12 --> 0x12
' 103 --> 0x103
' 255 --> 0x255
' Base 16 conversion:
' 0 --> 0x0
' 12 --> 0xc
' 103 --> 0x67
' 255 --> 0xff
let bases = [2; 8; 10; 16]
let numbers = [ Byte.MinValue; 12uy; 103uy; Byte.MaxValue ]
for baseValue in bases do
printfn "Base %i conversion:" baseValue
for number in numbers do
printfn " % -5i --> 0x%s" number (Convert.ToString (number, baseValue))
// The example displays the following output:
// Base 2 conversion:
// 0 --> 0x0
// 12 --> 0x1100
// 103 --> 0x1100111
// 255 --> 0x11111111
// Base 8 conversion:
// 0 --> 0x0
// 12 --> 0x14
// 103 --> 0x147
// 255 --> 0x377
// Base 10 conversion:
// 0 --> 0x0
// 12 --> 0x12
// 103 --> 0x103
// 255 --> 0x255
// Base 16 conversion:
// 0 --> 0x0
// 12 --> 0xc
// 103 --> 0x67
// 255 --> 0xff
Remarques
Si toBase elle n’est pas égale à 10, la chaîne retournée par la Convert.ToString(Byte, Int32) méthode représente value par sa magnitude uniquement. Si la méthode est appelée pour créer une chaîne qui sera convertie ultérieurement en nombre, une méthode correspondante qui suppose qu’une représentation numérique de magnitude uniquement doit être appelée pour effectuer la conversion. Ces méthodes incluent Convert.ToByte(String, Int32) ou Byte.Parse(String, NumberStyles).
S’applique à
ToString(Byte, IFormatProvider)
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
Convertit la valeur de l’entier non signé 8 bits spécifié en sa représentation sous forme de chaîne équivalente, à l’aide des informations de mise en forme spécifiques à la culture spécifiées.
public:
static System::String ^ ToString(System::Byte value, IFormatProvider ^ provider);
public static string ToString(byte value, IFormatProvider provider);
public static string ToString(byte value, IFormatProvider? provider);
static member ToString : byte * IFormatProvider -> string
Public Shared Function ToString (value As Byte, provider As IFormatProvider) As String
Paramètres
- value
- Byte
Entier non signé 8 bits à convertir.
- provider
- IFormatProvider
Objet qui fournit des informations de mise en forme propres à la culture.
Retours
Représentation sous forme de chaîne de value.
Exemples
L’exemple suivant convertit chaque élément d’un tableau d’octets non signé en sa représentation sous forme de chaîne équivalente à l’aide des conventions de mise en forme des cultures en-US et fr-FR. Étant donné que le spécificateur « G » par défaut génère uniquement des chiffres décimaux dans la représentation sous forme de chaîne d’une valeur d’octet, le provider paramètre n’affecte pas la mise en forme de la chaîne retournée.
byte[] numbers = { 12, 100, Byte.MaxValue };
// Define the culture names used to display them.
string[] cultureNames = { "en-US", "fr-FR" };
foreach (byte 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();
}
// The example displays the following output:
// 12:
// en-US: 12
// fr-FR: 12
//
// 100:
// en-US: 100
// fr-FR: 100
//
// 255:
// en-US: 255
// fr-FR: 255
let numbers = [| 12uy; 100uy; Byte.MaxValue |]
// Define the culture names used to display them.
let cultureNames = [| "en-US"; "fr-FR" |]
for number in numbers do
printfn $"{Convert.ToString(number, CultureInfo.InvariantCulture)}:"
for cultureName in cultureNames do
let culture = CultureInfo cultureName
printfn $" {culture.Name}: {Convert.ToString(number, culture),20}"
printfn ""
// The example displays the following output:
// 12:
// en-US: 12
// fr-FR: 12
//
// 100:
// en-US: 100
// fr-FR: 100
//
// 255:
// en-US: 255
// fr-FR: 255
' Define an array of numbers to display.
Dim numbers() As Byte = { 12, 100, Byte.MaxValue }
' Define the culture names used to display them.
Dim cultureNames() As String = { "en-US", "fr-FR" }
For Each number As Byte In numbers
Console.WriteLine("{0}:", Convert.ToString(number, _
System.Globalization.CultureInfo.InvariantCulture))
For Each cultureName As String In cultureNames
Dim culture As New System.Globalization.CultureInfo(cultureName)
Console.WriteLine(" {0}: {1,20}", _
culture.Name, Convert.ToString(number, culture))
Next
Console.WriteLine()
Next
' The example displays the following output:
' 12:
' en-US: 12
' fr-FR: 12
'
' 100:
' en-US: 100
' fr-FR: 100
'
' 255:
' en-US: 255
' fr-FR: 255
Remarques
Cette implémentation est identique à Byte.ToString.
S’applique à
ToString(Boolean, IFormatProvider)
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
Convertit la valeur booléenne spécifiée en sa représentation sous forme de chaîne équivalente.
public:
static System::String ^ ToString(bool value, IFormatProvider ^ provider);
public static string ToString(bool value, IFormatProvider provider);
public static string ToString(bool value, IFormatProvider? provider);
static member ToString : bool * IFormatProvider -> string
Public Shared Function ToString (value As Boolean, provider As IFormatProvider) As String
Paramètres
- value
- Boolean
Valeur booléenne à convertir.
- provider
- IFormatProvider
Instance d’un objet. Ce paramètre est ignoré.
Retours
Représentation sous forme de chaîne de value.
Exemples
L’exemple suivant convertit une Boolean valeur en une String avec la méthode, à l’aide ToString d’un IFormatProvider objet qui affiche le type du fournisseur de format pour lequel il est appelé. L’exemple montre que l’objet IFormatProvider n’est pas référencé.
// 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
*/
// Example of Convert.ToString(non-numeric types, IFormatProvider).
open System
open System.Globalization
// Create an instance of the IFormatProvider with an object expression.
let provider =
{ new IFormatProvider with
// Normally, GetFormat returns an object of the requested type
// (usually itself) if it is able; otherwise, it returns Nothing.
member _.GetFormat(argType: Type) =
// Here, the type of argType is displayed, and GetFormat
// always returns Nothing.
printf $"{argType,-40}"
null
}
// Convert these values using DummyProvider.
let Int32A = -252645135
let DoubleA = 61680.3855
let ObjDouble = -98765.4321 :> obj
let DayTimeA = DateTime(2001, 9, 11, 13, 45, 0)
let BoolA = true
let StringA = "Qwerty"
let CharA = '$'
let TSpanA = TimeSpan(0, 18, 0)
let ObjOther = provider :> obj
[<EntryPoint>]
let main _ =
printfn
"""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."""
// The format provider is called for these conversions.
printfn ""
let converted = Convert.ToString(Int32A, provider)
printfn $"int {converted}"
let converted = Convert.ToString(DoubleA, provider)
printfn $"double {converted}"
let converted = Convert.ToString(ObjDouble, provider)
printfn $"object {converted}"
let converted = Convert.ToString(DayTimeA, provider)
printfn $"DateTime {converted}"
// The format provider is not called for these conversions.
printfn ""
let converted = Convert.ToString(BoolA, provider)
printfn $"bool {converted}"
let converted = Convert.ToString(StringA, provider)
printfn $"string {converted}"
let converted = Convert.ToString(CharA, provider)
printfn $"char {converted}"
let converted = Convert.ToString(TSpanA, provider)
printfn $"TimeSpan {converted}"
let converted = Convert.ToString(ObjOther, provider)
printfn $"object {converted}"
0
// 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
' Example of Convert.ToString( non-numeric types, IFormatProvider ).
Imports System.Globalization
' An instance of this class can be passed to methods that require
' an IFormatProvider.
Public Class DummyProvider
Implements IFormatProvider
' Normally, GetFormat returns an object of the requested type
' (usually itself) if it is able; otherwise, it returns Nothing.
Public Function GetFormat( argType As Type ) As Object _
Implements IFormatProvider.GetFormat
' Here, the type of argType is displayed, and GetFormat
' always returns Nothing.
Console.Write( "{0,-40}", argType.ToString( ) )
Return Nothing
End Function
End Class
Module ConvertNonNumericProviderDemo
Sub Main( )
' Create an instance of the IFormatProvider.
Dim provider As New DummyProvider( )
Dim converted As String
' Convert these values using DummyProvider.
Dim Int32A As Integer = -252645135
Dim DoubleA As Double = 61680.3855
Dim ObjDouble As Object = CType( -98765.4321, Object )
Dim DayTimeA As DateTime = _
new DateTime( 2001, 9, 11, 13, 45, 0 )
Dim BoolA As Boolean = True
Dim StringA As String = "Qwerty"
Dim CharA As Char = "$"c
Dim TSpanA As TimeSpan = New TimeSpan( 0, 18, 0 )
Dim ObjOther As Object = CType( provider, Object )
Console.WriteLine( "This example of " & _
"Convert.ToString( non-numeric, IFormatProvider ) " & _
vbCrLf & "generates the following output. The " & _
"provider type, argument type, " & vbCrLf & "and " & _
"argument value are displayed." )
Console.WriteLine( vbCrLf & _
"Note: The IFormatProvider object is not called for " & _
"Boolean, String, " & vbCrLf & "Char, TimeSpan, " & _
"and non-numeric Object." )
' The format provider is called for these conversions.
Console.WriteLine( )
converted = Convert.ToString( Int32A, provider )
Console.WriteLine( "Int32 {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( "Boolean {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 )
End Sub
End Module
' 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 Int32 -252645135
' System.Globalization.NumberFormatInfo Double 61680.3855
' System.Globalization.NumberFormatInfo Object -98765.4321
' System.Globalization.DateTimeFormatInfo DateTime 9/11/2001 1:45:00 PM
'
' Boolean True
' String Qwerty
' Char $
' TimeSpan 00:18:00
' Object DummyProvider
Remarques
Cette implémentation est identique à Boolean.ToString. Elle retourne Boolean.TrueString des true valeurs et Boolean.FalseString des false valeurs.
S’applique à
ToString(Boolean)
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
Convertit la valeur booléenne spécifiée en sa représentation sous forme de chaîne équivalente.
public:
static System::String ^ ToString(bool value);
public static string ToString(bool value);
static member ToString : bool -> string
Public Shared Function ToString (value As Boolean) As String
Paramètres
- value
- Boolean
Valeur booléenne à convertir.
Retours
Représentation sous forme de chaîne de value.
Exemples
L’exemple suivant illustre la conversion d’un Boolean en un String, à l’aide ToStringde . Il illustre également que la chaîne retournée par la conversion est égale ou Boolean.TrueStringBoolean.FalseString.
bool falseFlag = false;
bool trueFlag = true;
Console.WriteLine(Convert.ToString(falseFlag));
Console.WriteLine(Convert.ToString(falseFlag).Equals(Boolean.FalseString));
Console.WriteLine(Convert.ToString(trueFlag));
Console.WriteLine(Convert.ToString(trueFlag).Equals(Boolean.TrueString));
// The example displays the following output:
// False
// True
// True
// True
let falseFlag = false
let trueFlag = true
printfn $"{Convert.ToString falseFlag}"
printfn $"{Convert.ToString(falseFlag).Equals Boolean.FalseString}"
printfn $"{Convert.ToString trueFlag}"
printfn $"{Convert.ToString(trueFlag).Equals(Boolean.TrueString)}"
// The example displays the following output:
// False
// True
// True
// True
Dim falseFlag As Boolean = False
Dim trueFlag As Boolean = True
Console.WriteLine(Convert.ToString(falseFlag))
Console.WriteLine(Convert.ToString(falseFlag).Equals(Boolean.FalseString))
Console.WriteLine(Convert.ToString(trueFlag))
Console.WriteLine(Convert.ToString(trueFlag).Equals(Boolean.TrueString))
' The example displays the following output:
' False
' True
' True
' True
let falseFlag = false
let trueFlag = true
Console.WriteLine (Convert.ToString falseFlag)
Console.WriteLine (Convert.ToString falseFlag=Boolean.FalseString)
Console.WriteLine (Convert.ToString trueFlag)
Console.WriteLine (Convert.ToString trueFlag=Boolean.TrueString)
// The example displays the following output:
// False
// True
// True
// True
Remarques
Cette implémentation est identique à Boolean.ToString. Elle retourne Boolean.TrueString des true valeurs et Boolean.FalseString des false valeurs.
S’applique à
ToString(Byte)
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
Convertit la valeur de l’entier non signé 8 bits spécifié en sa représentation sous forme de chaîne équivalente.
public:
static System::String ^ ToString(System::Byte value);
public static string ToString(byte value);
static member ToString : byte -> string
Public Shared Function ToString (value As Byte) As String
Paramètres
- value
- Byte
Entier non signé 8 bits à convertir.
Retours
Représentation sous forme de chaîne de value.
Exemples
L’exemple suivant convertit chaque valeur d’un Byte tableau en chaîne.
using System;
public class Example
{
public static void Main()
{
byte[] values = { Byte.MinValue, 12, 100, 179, Byte.MaxValue } ;
foreach (var value in values)
Console.WriteLine("{0,3} ({1}) --> {2}", value,
value.GetType().Name,
Convert.ToString(value));
}
}
// The example displays the following output:
// 0 (Byte) --> 0
// 12 (Byte) --> 12
// 100 (Byte) --> 100
// 179 (Byte) --> 179
// 255 (Byte) --> 255
open System
let values =
[| Byte.MinValue; 12uy; 100uy; 179uy; Byte.MaxValue |]
for value in values do
printfn $"{value,3} ({value.GetType().Name}) --> {Convert.ToString value}"
// The example displays the following output:
// 0 (Byte) --> 0
// 12 (Byte) --> 12
// 100 (Byte) --> 100
// 179 (Byte) --> 179
// 255 (Byte) --> 255
Public Module Example
Public Sub Main()
Dim values() As Byte = { Byte.MinValue, 12, 100, 179, Byte.MaxValue }
For Each value In values
Console.WriteLine("{0,3} ({1}) --> {2}", value,
value.GetType().Name,
Convert.ToString(value))
Next
End Sub
End Module
' The example displays the following output:
' 0 (Byte) --> 0
' 12 (Byte) --> 12
' 100 (Byte) --> 100
' 179 (Byte) --> 179
' 255 (Byte) --> 255
let values = [ Byte.MinValue; 12uy; 100uy; 179uy; Byte.MaxValue ]
for value in values do
printfn "% 4i (%s) --> %s" value (value.GetType().Name) (Convert.ToString value)
// The example displays the following output:
// 0 (Byte) --> 0
// 12 (Byte) --> 12
// 100 (Byte) --> 100
// 179 (Byte) --> 179
// 255 (Byte) --> 255
Remarques
Cette implémentation est identique à Byte.ToString().
S’applique à
ToString(Char)
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
Convertit la valeur du caractère Unicode spécifié en sa représentation sous forme de chaîne équivalente.
public:
static System::String ^ ToString(char value);
public static string ToString(char value);
static member ToString : char -> string
Public Shared Function ToString (value As Char) As String
Paramètres
- value
- Char
Caractère Unicode à convertir.
Retours
Représentation sous forme de chaîne de value.
Exemples
L’exemple suivant convertit un CharStringen .
public void ConvertStringChar(string stringVal) {
char charVal = 'a';
// A string must be one character long to convert to char.
try {
charVal = System.Convert.ToChar(stringVal);
System.Console.WriteLine("{0} as a char is {1}",
stringVal, charVal);
}
catch (System.FormatException) {
System.Console.WriteLine(
"The string is longer than one character.");
}
catch (System.ArgumentNullException) {
System.Console.WriteLine("The string is null.");
}
// A char to string conversion will always succeed.
stringVal = System.Convert.ToString(charVal);
System.Console.WriteLine("The character as a string is {0}",
stringVal);
}
let convertStringChar (stringVal: string) =
let charVal = 'a'
// A string must be one character long to convert to char.
try
let charVal = Convert.ToChar stringVal
printfn $"{stringVal} as a char is {charVal}"
with
| :? FormatException ->
printfn "The string is longer than one character."
| :? ArgumentNullException ->
printfn "The string is null."
// A char to string conversion will always succeed.
let stringVal = Convert.ToString charVal
printfn $"The character as a string is {stringVal}"
Public Sub ConvertStringChar(ByVal stringVal As String)
Dim charVal As Char = "a"c
' A string must be one character long to convert to char.
Try
charVal = System.Convert.ToChar(stringVal)
System.Console.WriteLine("{0} as a char is {1}", _
stringVal, charVal)
Catch exception As System.FormatException
System.Console.WriteLine( _
"The string is longer than one character.")
Catch exception As System.ArgumentNullException
System.Console.WriteLine("The string is null.")
End Try
' A char to string conversion will always succeed.
stringVal = System.Convert.ToString(charVal)
System.Console.WriteLine("The character as a string is {0}", _
stringVal)
End Sub
Remarques
Cette implémentation est identique à Char.ToString.
S’applique à
ToString(DateTime)
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
Convertit la valeur de l’élément spécifié DateTime en sa représentation sous forme de chaîne équivalente.
public:
static System::String ^ ToString(DateTime value);
public static string ToString(DateTime value);
static member ToString : DateTime -> string
Public Shared Function ToString (value As DateTime) As String
Paramètres
- value
- DateTime
Valeur de date et d’heure à convertir.
Retours
Représentation sous forme de chaîne de value.
Exemples
L’exemple suivant convertit chaque élément d’un tableau d’une DateTime valeur en String valeur.
DateTime[] dates = { new DateTime(2009, 7, 14),
new DateTime(1, 1, 1, 18, 32, 0),
new DateTime(2009, 2, 12, 7, 16, 0) };
string result;
foreach (DateTime dateValue in dates)
{
result = Convert.ToString(dateValue);
Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.",
dateValue.GetType().Name, dateValue,
result.GetType().Name, result);
}
// The example displays the following output:
// Converted the DateTime value 7/14/2009 12:00:00 AM to a String value 7/14/2009 12:00:00 AM.
// Converted the DateTime value 1/1/0001 06:32:00 PM to a String value 1/1/0001 06:32:00 PM.
// Converted the DateTime value 2/12/2009 07:16:00 AM to a String value 2/12/2009 07:16:00 AM.
let dates =
[| DateTime(2009, 7, 14)
DateTime(1, 1, 1, 18, 32, 0)
DateTime(2009, 2, 12, 7, 16, 0) |]
for dateValue in dates do
let result = Convert.ToString dateValue
printfn $"Converted the {dateValue.GetType().Name} value {dateValue} to the {result.GetType().Name} value {result}."
// The example displays the following output:
// Converted the DateTime value 7/14/2009 12:00:00 AM to a String value 7/14/2009 12:00:00 AM.
// Converted the DateTime value 1/1/0001 06:32:00 PM to a String value 1/1/0001 06:32:00 PM.
// Converted the DateTime value 2/12/2009 07:16:00 AM to a String value 2/12/2009 07:16:00 AM.
Dim dates() As Date = { #07/14/2009#, #6:32PM#, #02/12/2009 7:16AM#}
Dim result As String
For Each dateValue As Date In dates
result = Convert.ToString(dateValue)
Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", _
dateValue.GetType().Name, dateValue, _
result.GetType().Name, result)
Next
' The example displays the following output:
' Converted the DateTime value 7/14/2009 12:00:00 AM to a String value 7/14/2009 12:00:00 AM.
' Converted the DateTime value 1/1/0001 06:32:00 PM to a String value 1/1/0001 06:32:00 PM.
' Converted the DateTime value 2/12/2009 07:16:00 AM to a String value 2/12/2009 07:16:00 AM.
Remarques
Cette implémentation est identique à DateTime.ToString(). Il utilise les conventions de mise en forme de la culture actuelle et le spécificateur de format « G » pour convertir une DateTime valeur en sa représentation sous forme de chaîne.
S’applique à
ToString(Double)
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
Convertit la valeur du nombre à virgule flottante double précision spécifié en sa représentation sous forme de chaîne équivalente.
public:
static System::String ^ ToString(double value);
public static string ToString(double value);
static member ToString : double -> string
Public Shared Function ToString (value As Double) As String
Paramètres
- value
- Double
Nombre à virgule flottante double précision à convertir.
Retours
Représentation sous forme de chaîne de value.
Exemples
L’exemple suivant convertit un DoubleStringen .
public void ConvertDoubleString(double doubleVal) {
string stringVal;
// A conversion from Double to string cannot overflow.
stringVal = System.Convert.ToString(doubleVal);
System.Console.WriteLine("{0} as a string is: {1}",
doubleVal, stringVal);
try {
doubleVal = System.Convert.ToDouble(stringVal);
System.Console.WriteLine("{0} as a double is: {1}",
stringVal, doubleVal);
}
catch (System.OverflowException) {
System.Console.WriteLine(
"Conversion from string-to-double overflowed.");
}
catch (System.FormatException) {
System.Console.WriteLine(
"The string was not formatted as a double.");
}
catch (System.ArgumentException) {
System.Console.WriteLine(
"The string pointed to null.");
}
}
let convertDoubleString (doubleVal: float) =
// A conversion from Double to string cannot overflow.
let stringVal = Convert.ToString doubleVal
printfn $"{doubleVal} as a string is: {stringVal}"
try
let doubleVal = Convert.ToDouble stringVal
printfn $"{stringVal} as a double is: {doubleVal}"
with
| :? OverflowException ->
printfn "Conversion from string-to-double overflowed."
| :? FormatException ->
printfn "The string was not formatted as a double."
| :? ArgumentException ->
printfn "The string pointed to null."
Public Sub ConvertDoubleString(ByVal doubleVal As Double)
Dim stringVal As String
' A conversion from Double to String cannot overflow.
stringVal = System.Convert.ToString(doubleVal)
System.Console.WriteLine("{0} as a String is: {1}", _
doubleVal, stringVal)
Try
doubleVal = System.Convert.ToDouble(stringVal)
System.Console.WriteLine("{0} as a Double is: {1}", _
stringVal, doubleVal)
Catch exception As System.OverflowException
System.Console.WriteLine( _
"Overflow in String-to-Double conversion.")
Catch exception As System.FormatException
System.Console.WriteLine( _
"The string is not formatted as a Double.")
Catch exception As System.ArgumentException
System.Console.WriteLine("The string is null.")
End Try
End Sub
Remarques
Cette implémentation est identique à Double.ToString().
S’applique à
ToString(Int16)
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
Convertit la valeur de l’entier signé 16 bits spécifié en sa représentation sous forme de chaîne équivalente.
public:
static System::String ^ ToString(short value);
public static string ToString(short value);
static member ToString : int16 -> string
Public Shared Function ToString (value As Short) As String
Paramètres
- value
- Int16
Entier signé 16 bits à convertir.
Retours
Représentation sous forme de chaîne de value.
Exemples
L’exemple suivant convertit chaque élément d’un tableau d’entiers 16 bits en sa représentation sous forme de chaîne équivalente.
short[] numbers = { Int16.MinValue, -138, 0, 19, Int16.MaxValue };
string result;
foreach (short number in numbers)
{
result = Convert.ToString(number);
Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.",
number.GetType().Name, number,
result.GetType().Name, result);
}
// The example displays the following output:
// Converted the Int16 value -32768 to the String value -32768.
// Converted the Int16 value -138 to the String value -138.
// Converted the Int16 value 0 to the String value 0.
// Converted the Int16 value 19 to the String value 19.
// Converted the Int16 value 32767 to the String value 32767.
let numbers = [| Int16.MinValue; -138s; 0s; 19s; Int16.MaxValue |]
for number in numbers do
let result = Convert.ToString number
printfn $"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."
// The example displays the following output:
// Converted the Int16 value -32768 to the String value -32768.
// Converted the Int16 value -138 to the String value -138.
// Converted the Int16 value 0 to the String value 0.
// Converted the Int16 value 19 to the String value 19.
// Converted the Int16 value 32767 to the String value 32767.
Dim numbers() As Short = { Int16.MinValue, -138, 0, 19, Int16.MaxValue }
Dim result As String
For Each number As Short In numbers
result = Convert.ToString(number)
Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", _
number.GetType().Name, number, _
result.GetType().Name, result)
Next
' The example displays the following output:
' Converted the Int16 value -32768 to the String value -32768.
' Converted the Int16 value -138 to the String value -138.
' Converted the Int16 value 0 to the String value 0.
' Converted the Int16 value 19 to the String value 19.
' Converted the Int16 value 32767 to the String value 32767.
Remarques
Cette implémentation est identique à Int16.ToString().
S’applique à
ToString(Int32)
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
Convertit la valeur de l’entier signé 32 bits spécifié en sa représentation sous forme de chaîne équivalente.
public:
static System::String ^ ToString(int value);
public static string ToString(int value);
static member ToString : int -> string
Public Shared Function ToString (value As Integer) As String
Paramètres
- value
- Int32
Entier signé 32 bits à convertir.
Retours
Représentation sous forme de chaîne de value.
Exemples
L’exemple suivant compare la ToString(Int32) méthode à la ToString(UInt16, IFormatProvider) méthode. Il définit un objet personnalisé NumberFormatInfo qui utilise le signe « moins » de sting pour représenter le signe négatif. Il convertit chaque élément d’un tableau entier en sa représentation sous forme de chaîne équivalente à l’aide de la mise en forme par défaut (conventions de mise en forme de la culture actuelle) et du fournisseur de format personnalisé.
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
// Create a NumberFormatInfo object and set its NegativeSigns
// property to use for integer formatting.
NumberFormatInfo provider = new NumberFormatInfo();
provider.NegativeSign = "minus ";
int[] values = { -20, 0, 100 };
Console.WriteLine("{0,-8} --> {1,10} {2,10}\n", "Value",
CultureInfo.CurrentCulture.Name,
"Custom");
foreach (int value in values)
Console.WriteLine("{0,-8} --> {1,10} {2,10}",
value, Convert.ToString(value),
Convert.ToString(value, provider));
// The example displays output like the following:
// Value --> en-US Custom
//
// -20 --> -20 minus 20
// 0 --> 0 0
// 100 --> 100 100
}
}
open System
open System.Globalization
// Create a NumberFormatInfo object and set its NegativeSigns
// property to use for integer formatting.
let provider = NumberFormatInfo()
provider.NegativeSign <- "minus "
let values = [| -20; 0; 100 |]
printfn $"""{CultureInfo.CurrentCulture.Name,-8} --> {"Value",10} {"Custom",10}\n"""
for value in values do
printfn $"{value,-8} --> {Convert.ToString value,10} {Convert.ToString(value, provider),10}"
// The example displays output like the following:
// Value --> en-US Custom
//
// -20 --> -20 minus 20
// 0 --> 0 0
// 100 --> 100 100
Imports System.Globalization
Module Example
Public Sub Main()
' Create a NumberFormatInfo object and set its NegativeSigns
' property to use for integer formatting.
Dim provider As New NumberFormatInfo()
provider.NegativeSign = "minus "
Dim values() As Integer = { -20, 0, 100 }
Console.WriteLine("{0,-8} --> {1,10} {2,10}", "Value",
CultureInfo.CurrentCulture.Name,
"Custom")
Console.WriteLine()
For Each value As Integer In values
Console.WriteLine("{0,-8} --> {1,10} {2,10}",
value, Convert.ToString(value),
Convert.ToString(value, provider))
Next
End Sub
End Module
' The example displays output like the following:
' Value --> en-US Custom
'
' -20 --> -20 minus 20
' 0 --> 0 0
' 100 --> 100 100
Remarques
Cette implémentation est identique à Int32.ToString(). Il met en value forme à l’aide des conventions de mise en forme de la culture actuelle.
S’applique à
ToString(Decimal)
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
Convertit la valeur du nombre décimal spécifié en sa représentation sous forme de chaîne équivalente.
public:
static System::String ^ ToString(System::Decimal value);
public static string ToString(decimal value);
static member ToString : decimal -> string
Public Shared Function ToString (value As Decimal) As String
Paramètres
- value
- Decimal
Nombre décimal à convertir.
Retours
Représentation sous forme de chaîne de value.
Exemples
L’exemple suivant convertit un DecimalStringen .
public void ConvertStringDecimal(string stringVal) {
decimal decimalVal = 0;
try {
decimalVal = System.Convert.ToDecimal(stringVal);
System.Console.WriteLine(
"The string as a decimal is {0}.", decimalVal);
}
catch (System.OverflowException){
System.Console.WriteLine(
"The conversion from string to decimal overflowed.");
}
catch (System.FormatException) {
System.Console.WriteLine(
"The string is not formatted as a decimal.");
}
catch (System.ArgumentNullException) {
System.Console.WriteLine(
"The string is null.");
}
// Decimal to string conversion will not overflow.
stringVal = System.Convert.ToString(decimalVal);
System.Console.WriteLine(
"The decimal as a string is {0}.", stringVal);
}
let convertStringDecimal (stringVal: string) =
let decimalVal = 0m
try
let decimalVal = Convert.ToDecimal(stringVal)
printfn $"The string as a decimal is {decimalVal}."
with
| :? OverflowException ->
printfn "The conversion from string to decimal overflowed."
| :? FormatException ->
printfn "The string is not formatted as a decimal."
| :? ArgumentNullException ->
printfn "The string is null."
// Decimal to string conversion will not overflow.
let stringVal = Convert.ToString decimalVal
printfn $"The decimal as a string is {stringVal}."
Public Sub ConvertStringDecimal(ByVal stringVal As String)
Dim decimalVal As Decimal = 0
Try
decimalVal = System.Convert.ToDecimal(stringVal)
System.Console.WriteLine("The string as a decimal is {0}.", _
decimalVal)
Catch exception As System.OverflowException
System.Console.WriteLine( _
"Overflow in string-to-decimal conversion.")
Catch exception As System.FormatException
System.Console.WriteLine( _
"The string is not formatted as a decimal.")
Catch exception As System.ArgumentException
System.Console.WriteLine("The string is null.")
End Try
' Decimal to string conversion will not overflow.
stringVal = System.Convert.ToString(decimalVal)
System.Console.WriteLine("The decimal as a string is {0}.", _
stringVal)
End Sub
Remarques
Cette implémentation est identique à Decimal.ToString().
S’applique à
ToString(Object)
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
Convertit la valeur de l’objet spécifié en sa représentation sous forme de chaîne équivalente.
public:
static System::String ^ ToString(System::Object ^ value);
public static string ToString(object value);
public static string? ToString(object? value);
static member ToString : obj -> string
Public Shared Function ToString (value As Object) As String
Paramètres
- value
- Object
Objet qui fournit la valeur à convertir, ou null.
Retours
Représentation sous forme de chaîne de value, ou Empty le cas value échéant null.
Exemples
L’exemple suivant convertit chaque élément d’un tableau d’objets en sa représentation sous forme de chaîne équivalente.
object[] values = { false, 12.63m, new DateTime(2009, 6, 1, 6, 32, 15), 16.09e-12,
'Z', 15.15322, SByte.MinValue, Int32.MaxValue };
string result;
foreach (object value in values)
{
result = Convert.ToString(value);
Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.",
value.GetType().Name, value,
result.GetType().Name, result);
}
// The example displays the following output:
// Converted the Boolean value False to the String value False.
// Converted the Decimal value 12.63 to the String value 12.63.
// Converted the DateTime value 6/1/2009 06:32:15 AM to the String value 6/1/2009 06:32:15 AM.
// Converted the Double value 1.609E-11 to the String value 1.609E-11.
// Converted the Char value Z to the String value Z.
// Converted the Double value 15.15322 to the String value 15.15322.
// Converted the SByte value -128 to the String value -128.
// Converted the Int32 value 2147483647 to the String value 2147483647.
let values: obj[] =
[| false; 12.63m; DateTime(2009, 6, 1, 6, 32, 15)
16.09e-12; 'Z'; 15.15322; SByte.MinValue; Int32.MaxValue |]
for value in values do
let result = Convert.ToString value
printfn $"Converted the {value.GetType().Name} value {value} to the {result.GetType().Name} value {result}."
// The example displays the following output:
// Converted the Boolean value False to the String value False.
// Converted the Decimal value 12.63 to the String value 12.63.
// Converted the DateTime value 6/1/2009 06:32:15 AM to the String value 6/1/2009 06:32:15 AM.
// Converted the Double value 1.609E-11 to the String value 1.609E-11.
// Converted the Char value Z to the String value Z.
// Converted the Double value 15.15322 to the String value 15.15322.
// Converted the SByte value -128 to the String value -128.
// Converted the Int32 value 2147483647 to the String value 2147483647.
Dim values() As Object = { False, 12.63d, #06/01/2009 6:32:15#, 16.09e-12, _
"Z"c, 15.15322, SByte.MinValue, Int32.MaxValue}
Dim result As String
For Each value As Object In values
result = Convert.ToString(value)
Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", _
value.GetType().Name, value, _
result.GetType().Name, result)
Next
' The example displays the following output:
' Converted the Boolean value False to the String value False.
' Converted the Decimal value 12.63 to the String value 12.63.
' Converted the DateTime value 6/1/2009 06:32:15 AM to the String value 6/1/2009 06:32:15 AM.
' Converted the Double value 1.609E-11 to the String value 1.609E-11.
' Converted the Char value Z to the String value Z.
' Converted the Double value 15.15322 to the String value 15.15322.
' Converted the SByte value -128 to the String value -128.
' Converted the Int32 value 2147483647 to the String value 2147483647.
Remarques
Pour convertir value en sa représentation sous forme de chaîne, la méthode tente d’appeler l’implémentation IConvertible.ToString de value. Si value elle n’implémente pas l’interface IConvertible , la méthode tente d’appeler l’implémentation IFormattable.ToString de value. Si la valeur n’implémente pas l’interface IFormattable , la méthode appelle la ToString méthode du type sous-jacent de value.
S’applique à
ToString(SByte)
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
Important
Cette API n’est pas conforme CLS.
Convertit la valeur de l’entier signé 8 bits spécifié en sa représentation sous forme de chaîne équivalente.
public:
static System::String ^ ToString(System::SByte value);
[System.CLSCompliant(false)]
public static string ToString(sbyte value);
[<System.CLSCompliant(false)>]
static member ToString : sbyte -> string
Public Shared Function ToString (value As SByte) As String
Paramètres
- value
- SByte
Entier signé 8 bits à convertir.
Retours
Représentation sous forme de chaîne de value.
- Attributs
Exemples
L’exemple suivant convertit chaque élément d’un tableau d’octets signé en sa représentation sous forme de chaîne équivalente.
sbyte[] numbers = { SByte.MinValue, -12, 0, 16, SByte.MaxValue };
string result;
foreach (sbyte number in numbers)
{
result = Convert.ToString(number);
Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.",
number.GetType().Name, number,
result.GetType().Name, result);
}
// The example displays the following output:
// Converted the SByte value -128 to the String value -128.
// Converted the SByte value -12 to the String value -12.
// Converted the SByte value 0 to the String value 0.
// Converted the SByte value 16 to the String value 16.
// Converted the SByte value 127 to the String value 127.
let numbers = [| SByte.MinValue; -12y; 0y; 16y; SByte.MaxValue |]
for number in numbers do
let result = Convert.ToString number
printfn $"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."
// The example displays the following output:
// Converted the SByte value -128 to the String value -128.
// Converted the SByte value -12 to the String value -12.
// Converted the SByte value 0 to the String value 0.
// Converted the SByte value 16 to the String value 16.
// Converted the SByte value 127 to the String value 127.
Dim numbers() As SByte = { SByte.MinValue, -12, 0, 16, SByte.MaxValue }
Dim result As String
For Each number As SByte In numbers
result = Convert.ToString(number)
Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", _
number.GetType().Name, number, _
result.GetType().Name, result)
Next
' The example displays the following output:
' Converted the SByte value -128 to the String value -128.
' Converted the SByte value -12 to the String value -12.
' Converted the SByte value 0 to the String value 0.
' Converted the SByte value 16 to the String value 16.
' Converted the SByte value 127 to the String value 127.
Remarques
Cette implémentation est identique à SByte.ToString().
S’applique à
ToString(Single)
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
Convertit la valeur du nombre à virgule flottante simple précision spécifié en sa représentation sous forme de chaîne équivalente.
public:
static System::String ^ ToString(float value);
public static string ToString(float value);
static member ToString : single -> string
Public Shared Function ToString (value As Single) As String
Paramètres
- value
- Single
Nombre à virgule flottante simple précision à convertir.
Retours
Représentation sous forme de chaîne de value.
Exemples
L’exemple suivant convertit chaque élément d’un tableau de valeurs en sa représentation sous forme de Single chaîne équivalente.
float[] numbers = { Single.MinValue, -1011.351f, -17.45f, -3e-16f,
0f, 4.56e-12f, 16.0001f, 10345.1221f, Single.MaxValue };
string result;
foreach (float number in numbers)
{
result = Convert.ToString(number);
Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.",
number.GetType().Name, number,
result.GetType().Name, result);
}
// The example displays the following output:
// Converted the Single value -3.402823E+38 to the String value -3.402823E+38.
// Converted the Single value -1011.351 to the String value -1011.351.
// Converted the Single value -17.45 to the String value -17.45.
// Converted the Single value -3E-16 to the String value -3E-16.
// Converted the Single value 0 to the String value 0.
// Converted the Single value 4.56E-12 to the String value 4.56E-12.
// Converted the Single value 16.0001 to the String value 16.0001.
// Converted the Single value 10345.12 to the String value 10345.12.
// Converted the Single value 3.402823E+38 to the String value 3.402823E+38.
let numbers =
[| Single.MinValue; -1011.351f; -17.45f; -3e-16f; 0f; 4.56e-12f; 16.0001f; 10345.1221f; Single.MaxValue |]
for number in numbers do
let result = Convert.ToString number
printfn $"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."
// The example displays the following output:
// Converted the Single value -3.402823E+38 to the String value -3.402823E+38.
// Converted the Single value -1011.351 to the String value -1011.351.
// Converted the Single value -17.45 to the String value -17.45.
// Converted the Single value -3E-16 to the String value -3E-16.
// Converted the Single value 0 to the String value 0.
// Converted the Single value 4.56E-12 to the String value 4.56E-12.
// Converted the Single value 16.0001 to the String value 16.0001.
// Converted the Single value 10345.12 to the String value 10345.12.
// Converted the Single value 3.402823E+38 to the String value 3.402823E+38.
Dim numbers() As Single = { Single.MinValue, -1011.351, -17.45, -3e-16, _
0, 4.56e-12, 16.0001, 10345.1221, Single.MaxValue }
Dim result As String
For Each number As Single In numbers
result = Convert.ToString(number)
Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", _
number.GetType().Name, number, _
result.GetType().Name, result)
Next
' The example displays the following output:
' Converted the Single value -3.402823E+38 to the String value -3.402823E+38.
' Converted the Single value -1011.351 to the String value -1011.351.
' Converted the Single value -17.45 to the String value -17.45.
' Converted the Single value -3E-16 to the String value -3E-16.
' Converted the Single value 0 to the String value 0.
' Converted the Single value 4.56E-12 to the String value 4.56E-12.
' Converted the Single value 16.0001 to the String value 16.0001.
' Converted the Single value 10345.12 to the String value 10345.12.
' Converted the Single value 3.402823E+38 to the String value 3.402823E+38.
Remarques
Cette implémentation est identique à Single.ToString().
S’applique à
ToString(String)
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
Retourne l’instance de chaîne spécifiée ; aucune conversion réelle n’est effectuée.
public:
static System::String ^ ToString(System::String ^ value);
public static string? ToString(string? value);
public static string ToString(string value);
static member ToString : string -> string
Public Shared Function ToString (value As String) As String
Paramètres
- value
- String
Chaîne à retourner.
Retours
value est retourné inchangé.
Exemples
L’exemple suivant transmet une chaîne à la ToString(String) méthode et appelle la Object.ReferenceEquals méthode pour confirmer que la méthode retourne la chaîne d’origine. L’exemple appelle également la String.IsInterned méthode pour s’assurer que les deux chaînes ne sont pas identiques, car la chaîne d’origine est interne.
using System;
public class Example
{
public static void Main()
{
String article = "An";
String noun = "apple";
String str1 = String.Format("{0} {1}", article, noun);
String str2 = Convert.ToString(str1);
Console.WriteLine("str1 is interned: {0}",
! (String.IsInterned(str1) == null));
Console.WriteLine("str1 and str2 are the same reference: {0}",
Object.ReferenceEquals(str1, str2));
}
}
// The example displays the following output:
// str1 is interned: False
// str1 and str2 are the same reference: True
open System
let article = "An"
let noun = "apple"
let str1 = $"{article} {noun}"
let str2 = Convert.ToString str1
printfn $"str1 is interned: {String.IsInterned str1 <> null}"
printfn $"str1 and str2 are the same reference: {Object.ReferenceEquals(str1, str2)}"
// The example displays the following output:
// str1 is interned: False
// str1 and str2 are the same reference: True
Module Example
Public Sub Main()
Dim article As String = "An"
Dim noun As String = "apple"
Dim str1 As String = String.Format("{0} {1}", article, noun)
Dim str2 As String = Convert.ToString(str1)
Console.WriteLine("str1 is interned: {0}",
Not String.IsInterned(str1) Is Nothing)
Console.WriteLine("str1 and str2 are the same reference: {0}",
Object.ReferenceEquals(str1, str2))
End Sub
End Module
' The example displays the following output:
' str1 is interned: False
' str1 and str2 are the same reference: True
S’applique à
ToString(UInt16)
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
Important
Cette API n’est pas conforme CLS.
Convertit la valeur de l’entier non signé 16 bits spécifié en sa représentation sous forme de chaîne équivalente.
public:
static System::String ^ ToString(System::UInt16 value);
[System.CLSCompliant(false)]
public static string ToString(ushort value);
[<System.CLSCompliant(false)>]
static member ToString : uint16 -> string
Public Shared Function ToString (value As UShort) As String
Paramètres
- value
- UInt16
Entier non signé 16 bits à convertir.
Retours
Représentation sous forme de chaîne de value.
- Attributs
Exemples
L’exemple suivant convertit chaque élément dans un tableau de valeurs entières 16 bits non signées en sa représentation sous forme de chaîne équivalente.
ushort[] numbers = { UInt16.MinValue, 103, 1045, UInt16.MaxValue };
string result;
foreach (ushort number in numbers)
{
result = Convert.ToString(number);
Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.",
number.GetType().Name, number,
result.GetType().Name, result);
}
// The example displays the following output:
// Converted the UInt16 value 0 to the String value 0.
// Converted the UInt16 value 103 to the String value 103.
// Converted the UInt16 value 1045 to the String value 1045.
// Converted the UInt16 value 65535 to the String value 65535.
let numbers = [| UInt16.MinValue; 103us; 1045us; UInt16.MaxValue |]
for number in numbers do
let result = Convert.ToString number
printfn $"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."
// The example displays the following output:
// Converted the UInt16 value 0 to the String value 0.
// Converted the UInt16 value 103 to the String value 103.
// Converted the UInt16 value 1045 to the String value 1045.
// Converted the UInt16 value 65535 to the String value 65535.
Dim numbers() As UShort = { UInt16.MinValue, 103, 1045, UInt16.MaxValue }
Dim result As String
For Each number As UShort In numbers
result = Convert.ToString(number)
Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", _
number.GetType().Name, number, _
result.GetType().Name, result)
Next
' The example displays the following output:
' Converted the UInt16 value 0 to the String value 0.
' Converted the UInt16 value 103 to the String value 103.
' Converted the UInt16 value 1045 to the String value 1045.
' Converted the UInt16 value 65535 to the String value 65535.
Remarques
Cette implémentation est identique à UInt16.ToString().
S’applique à
ToString(UInt32)
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
Important
Cette API n’est pas conforme CLS.
Convertit la valeur de l’entier non signé 32 bits spécifié en sa représentation sous forme de chaîne équivalente.
public:
static System::String ^ ToString(System::UInt32 value);
[System.CLSCompliant(false)]
public static string ToString(uint value);
[<System.CLSCompliant(false)>]
static member ToString : uint32 -> string
Public Shared Function ToString (value As UInteger) As String
Paramètres
- value
- UInt32
Entier non signé 32 bits à convertir.
Retours
Représentation sous forme de chaîne de value.
- Attributs
Exemples
L’exemple suivant convertit chaque élément d’un tableau entier non signé en sa représentation sous forme de chaîne équivalente.
uint[] numbers = { UInt32.MinValue, 103, 1045, 119543, UInt32.MaxValue };
string result;
foreach (uint number in numbers)
{
result = Convert.ToString(number);
Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.",
number.GetType().Name, number,
result.GetType().Name, result);
}
// The example displays the following output:
// Converted the UInt32 value 0 to the String value 0.
// Converted the UInt32 value 103 to the String value 103.
// Converted the UInt32 value 1045 to the String value 1045.
// Converted the UInt32 value 119543 to the String value 119543.
// Converted the UInt32 value 4294967295 to the String value 4294967295.
let numbers = [| UInt32.MinValue; 103u; 1045u; 119543u; UInt32.MaxValue |]
for number in numbers do
let result = Convert.ToString number
printfn $"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."
// The example displays the following output:
// Converted the UInt32 value 0 to the String value 0.
// Converted the UInt32 value 103 to the String value 103.
// Converted the UInt32 value 1045 to the String value 1045.
// Converted the UInt32 value 119543 to the String value 119543.
// Converted the UInt32 value 4294967295 to the String value 4294967295.
Dim numbers() As UInteger = { UInt32.MinValue, 103, 1045, 119543, UInt32.MaxValue }
Dim result As String
For Each number As UInteger In numbers
result = Convert.ToString(number)
Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", _
number.GetType().Name, number, _
result.GetType().Name, result)
Next
' The example displays the following output:
' Converted the UInt32 value 0 to the String value 0.
' Converted the UInt32 value 103 to the String value 103.
' Converted the UInt32 value 1045 to the String value 1045.
' Converted the UInt32 value 119543 to the String value 119543.
' Converted the UInt32 value 4294967295 to the String value 4294967295.
Remarques
Cette implémentation est identique à UInt32.ToString().
S’applique à
ToString(UInt64)
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
Important
Cette API n’est pas conforme CLS.
Convertit la valeur de l’entier non signé 64 bits spécifié en sa représentation sous forme de chaîne équivalente.
public:
static System::String ^ ToString(System::UInt64 value);
[System.CLSCompliant(false)]
public static string ToString(ulong value);
[<System.CLSCompliant(false)>]
static member ToString : uint64 -> string
Public Shared Function ToString (value As ULong) As String
Paramètres
- value
- UInt64
Entier non signé 64 bits à convertir.
Retours
Représentation sous forme de chaîne de value.
- Attributs
Exemples
L’exemple suivant convertit chaque élément d’un tableau entier long non signé en sa représentation sous forme de chaîne équivalente.
ulong[] numbers = { UInt64.MinValue, 1031, 189045, UInt64.MaxValue };
string result;
foreach (ulong number in numbers)
{
result = Convert.ToString(number);
Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.",
number.GetType().Name, number,
result.GetType().Name, result);
}
// The example displays the following output:
// Converted the UInt64 value 0 to the String value 0.
// Converted the UInt64 value 1031 to the String value 1031.
// Converted the UInt64 value 189045 to the String value 189045.
// Converted the UInt64 value 18446744073709551615 to the String value 18446744073709551615.
let numbers = [| UInt64.MinValue; 1031uL; 189045uL; UInt64.MaxValue |]
for number in numbers do
let result = Convert.ToString number
printfn $"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."
// The example displays the following output:
// Converted the UInt64 value 0 to the String value 0.
// Converted the UInt64 value 1031 to the String value 1031.
// Converted the UInt64 value 189045 to the String value 189045.
// Converted the UInt64 value 18446744073709551615 to the String value 18446744073709551615.
Dim numbers() As ULong = { UInt64.MinValue, 1031, 189045, UInt64.MaxValue }
Dim result As String
For Each number As ULong In numbers
result = Convert.ToString(number)
Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", _
number.GetType().Name, number, _
result.GetType().Name, result)
Next
' The example displays the following output:
' Converted the UInt64 value 0 to the String value 0.
' Converted the UInt64 value 1031 to the String value 1031.
' Converted the UInt64 value 189045 to the String value 189045.
' Converted the UInt64 value 18446744073709551615 to the String value 18446744073709551615.
Remarques
Cette implémentation est identique à UInt64.ToString().
S’applique à
ToString(Int64)
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
- Source:
- Convert.cs
Convertit la valeur de l’entier signé 64 bits spécifié en sa représentation sous forme de chaîne équivalente.
public:
static System::String ^ ToString(long value);
public static string ToString(long value);
static member ToString : int64 -> string
Public Shared Function ToString (value As Long) As String
Paramètres
- value
- Int64
Entier signé 64 bits à convertir.
Retours
Représentation sous forme de chaîne de value.
Exemples
L’exemple suivant convertit chaque élément d’un tableau entier long en sa représentation sous forme de chaîne équivalente.
// Create a NumberFormatInfo object and set several of its
// properties that control default integer formatting.
System.Globalization.NumberFormatInfo provider = new System.Globalization.NumberFormatInfo();
provider.NegativeSign = "minus ";
long[] values = { -200, 0, 1000 };
foreach (long value in values)
Console.WriteLine("{0,-6} --> {1,10}",
value, Convert.ToString(value, provider));
// The example displays the following output:
// -200 --> minus 200
// 0 --> 0
// 1000 --> 1000
// Create a NumberFormatInfo object and set several of its
// properties that control default integer formatting.
let provider = System.Globalization.NumberFormatInfo()
provider.NegativeSign <- "minus "
let values = [| -200; 0; 1000 |]
for value in values do
printfn $"{value,-6} --> {Convert.ToString(value, provider),10}"
// The example displays the following output:
// -200 --> minus 200
// 0 --> 0
// 1000 --> 1000
' Create a NumberFormatInfo object and set several of its
' properties that control default integer formatting.
Dim provider As New System.Globalization.NumberFormatInfo()
provider.NegativeSign = "minus "
Dim values() As Long = { -200, 0, 1000 }
For Each value As Long In values
Console.WriteLine("{0,-6} --> {1,10}", _
value, Convert.ToString(value, provider))
Next
' The example displays the following output:
' -200 --> minus 200
' 0 --> 0
' 1000 --> 1000
Remarques
Cette implémentation est identique à Int64.ToString().