次の方法で共有


Byte.ToString メソッド (IFormatProvider)

指定したカルチャ固有の書式情報を使用して、このインスタンスの数値を、それと等価の文字列に変換します。

Overloads Public Overridable Function ToString( _
   ByVal provider As IFormatProvider _) As String
[C#]
public virtual string ToString(IFormatProviderprovider);
[C++]
public: virtual String* ToString(IFormatProvider* provider);
[JScript]
public function ToString(
   provider : IFormatProvider) : String;

パラメータ

  • provider
    カルチャ固有の書式情報を提供する IFormatProvider

戻り値

provider で指定されたとおりに書式設定されたこのインスタンスの値。

解説

このインスタンスは一般書式指定子 ("G") で書式設定されます。

provider パラメータは、 NumberFormatInfo オブジェクトを提供する IFormatProvider インスタンスです。 NumberFormatInfo オブジェクトは、このインスタンスに関するカルチャ固有の情報を提供します。 provider が null 参照 (Visual Basic では Nothing) の場合や、 provider から NumberFormatInfo を取得できない場合は、このインスタンスは、現在のシステムのカルチャの NumberFormatInfo で書式設定されます。

使用例

[Visual Basic, C#, C++] ToString メソッドの複数のオーバーロードを使用して Byte 値を書式設定するコード例を次に示します。

 
' Example for the Byte.ToString( ) methods.
Imports System
Imports System.Globalization
Imports Microsoft.VisualBasic

Module ByteToStringDemo
    
    Sub RunToStringDemo( )

        Dim smallValue As Byte = 13
        Dim largeValue As Byte = 234
            
        ' Format the Byte values without and with format strings.
        Console.WriteLine( vbCrLf & "IFormatProvider is not used:" )
        Console.WriteLine( "   {0,-20}{1,10}{2,10}", _
            "No format string:", smallValue.ToString( ), _
            largeValue.ToString( ) )
        Console.WriteLine( "   {0,-20}{1,10}{2,10}", _
            "'X2' format string:", smallValue.ToString( "X2" ), _
            largeValue.ToString( "X2" ) )
            
        ' Get the NumberFormatInfo object from the 
        ' invariant culture.
        Dim culture As New CultureInfo( "" )
        Dim numInfo As NumberFormatInfo = culture.NumberFormat
            
        ' Set the digit grouping to 1, set the digit separator
        ' to underscore, and set decimal digits to 0.
        numInfo.NumberGroupSizes = New Integer( ) { 1 }
        numInfo.NumberGroupSeparator = "_"
        numInfo.NumberDecimalDigits = 0
            
        ' Use the NumberFormatInfo object for an IFormatProvider.
        Console.WriteLine( vbCrLf & _
            "A NumberFormatInfo object with digit group " & _
            "size = 1 and " & vbCrLf & "digit separator " & _
            "= '_' is used for the IFormatProvider:" )
        Console.WriteLine( "   {0,-20}{1,10}{2,10}", _
            "No format string:", smallValue.ToString( numInfo ), _
            largeValue.ToString( numInfo ) )
        Console.WriteLine( "   {0,-20}{1,10}{2,10}", _
            "'N' format string:", _
            smallValue.ToString( "N", numInfo ), _
            largeValue.ToString( "N", numInfo ) )
    End Sub 
        
    Sub Main( )

        Console.WriteLine( "This example of" & vbCrLf & _
            "   Byte.ToString( )," & vbCrLf & _ 
            "   Byte.ToString( String )," & vbCrLf & _
            "   Byte.ToString( IFormatProvider ), and" & vbCrLf & _
            "   Byte.ToString( String, IFormatProvider )" & _
            vbCrLf & "generates the following output when " & _
            "formatting Byte values " & vbCrLf & _
            "with combinations of format " & _
            "strings and IFormatProvider." )
            
        RunToStringDemo( )

    End Sub
End Module 

' This example of
'    Byte.ToString( ),
'    Byte.ToString( String ),
'    Byte.ToString( IFormatProvider ), and
'    Byte.ToString( String, IFormatProvider )
' generates the following output when formatting Byte values
' with combinations of format strings and IFormatProvider.
' 
' IFormatProvider is not used:
'    No format string:           13       234
'    'X2' format string:         0D        EA
'
' A NumberFormatInfo object with digit group size = 1 and
' digit separator = '_' is used for the IFormatProvider:
'    No format string:           13       234
'    'N' format string:         1_3     2_3_4

[C#] 
// Example for the Byte.ToString( ) methods.
using System;
using System.Globalization;

class ByteToStringDemo
{
    static void RunToStringDemo( )
    {
        byte smallValue = 13;
        byte largeValue = 234;
            
        // Format the Byte values without and with format strings.
        Console.WriteLine( "\nIFormatProvider is not used:" );
        Console.WriteLine( "   {0,-20}{1,10}{2,10}", 
            "No format string:", smallValue.ToString( ), 
            largeValue.ToString( ) );
        Console.WriteLine( "   {0,-20}{1,10}{2,10}", 
            "'X2' format string:", smallValue.ToString( "X2" ), 
            largeValue.ToString( "X2" ) );
            
        // Get the NumberFormatInfo object from the 
        // invariant culture.
        CultureInfo         culture = new CultureInfo( "" );
        NumberFormatInfo    numInfo = culture.NumberFormat;
            
        // Set the digit grouping to 1, set the digit separator
        // to underscore, and set decimal digits to 0.
        numInfo.NumberGroupSizes = new int[ ] { 1 };
        numInfo.NumberGroupSeparator = "_";
        numInfo.NumberDecimalDigits = 0;
            
        // Use the NumberFormatInfo object for an IFormatProvider.
        Console.WriteLine( 
            "\nA NumberFormatInfo object with digit group " +
            "size = 1 and \ndigit separator " +
            "= '_' is used for the IFormatProvider:" );
        Console.WriteLine( "   {0,-20}{1,10}{2,10}", 
            "No format string:", smallValue.ToString( numInfo ), 
            largeValue.ToString( numInfo ) );
        Console.WriteLine( "   {0,-20}{1,10}{2,10}", 
            "'N' format string:", 
            smallValue.ToString( "N", numInfo ), 
            largeValue.ToString( "N", numInfo ) );
    } 
        
    static void Main( )
    {
        Console.WriteLine( "This example of\n" +
            "   Byte.ToString( ),\n" +
            "   Byte.ToString( String ),\n" + 
            "   Byte.ToString( IFormatProvider ), and\n" +
            "   Byte.ToString( String, IFormatProvider )\n" +
            "generates the following output when formatting " +
            "Byte values \nwith combinations of format " +
            "strings and IFormatProvider." );
            
        RunToStringDemo( );
    }
} 

/*
This example of
   Byte.ToString( ),
   Byte.ToString( String ),
   Byte.ToString( IFormatProvider ), and
   Byte.ToString( String, IFormatProvider )
generates the following output when formatting Byte values
with combinations of format strings and IFormatProvider.

IFormatProvider is not used:
   No format string:           13       234
   'X2' format string:         0D        EA

A NumberFormatInfo object with digit group size = 1 and
digit separator = '_' is used for the IFormatProvider:
   No format string:           13       234
   'N' format string:         1_3     2_3_4
*/

[C++] 
// Example for the Byte::ToString( ) methods.
#using <mscorlib.dll>
using namespace System;
using namespace System::Globalization;

void RunToStringDemo( )
{    
    Byte smallValue = 13;
    Byte largeValue = 234;

    // Format the Byte values without and with format strings.
    Console::WriteLine( S"\nIFormatProvider is not used:" );
    Console::WriteLine( S"   {0,-20}{1,10}{2,10}", 
        S"No format string:", smallValue.ToString( ), 
        largeValue.ToString( ) );
    Console::WriteLine( S"   {0,-20}{1,10}{2,10}", 
        S"'X2' format string:", smallValue.ToString( S"X2" ), 
        largeValue.ToString( S"X2" ) );

    // Get the NumberFormatInfo object from the invariant culture.
    CultureInfo*        culture = new CultureInfo( S"" );
    NumberFormatInfo*   numInfo = culture->NumberFormat;

    // Set the digit grouping to 1, set the digit separator
    // to underscore, and set decimal digits to 0.
    Int32 sizes __gc [] = { 1 };
    numInfo->NumberGroupSizes = sizes;
    numInfo->NumberGroupSeparator = S"_";
    numInfo->NumberDecimalDigits = 0;

    // Use the NumberFormatInfo object for an IFormatProvider.
    Console::WriteLine( 
        S"\nA NumberFormatInfo object with digit group " 
        S"size = 1 and \ndigit separator " 
        S"= '_' is used for the IFormatProvider:" );
    Console::WriteLine( S"   {0,-20}{1,10}{2,10}", 
        S"No format string:", smallValue.ToString( numInfo ), 
        largeValue.ToString( numInfo ) );
    Console::WriteLine( S"   {0,-20}{1,10}{2,10}", 
        S"'N' format string:", smallValue.ToString( S"N", numInfo ), 
        largeValue.ToString( S"N", numInfo ) );
}

void main( )
{    
    Console::WriteLine( 
        S"This example of\n   Byte::ToString( ),\n" 
        S"   Byte::ToString( String* ),\n" 
        S"   Byte::ToString( IFormatProvider* ), and\n" 
        S"   Byte::ToString( String*, IFormatProvider* )\n" 
        S"generates the following output when formatting " 
        S"Byte values \nwith combinations of format " 
        S"strings and IFormatProvider." );
    
    RunToStringDemo( );
}

/*
This example of
   Byte::ToString( ),
   Byte::ToString( String* ),
   Byte::ToString( IFormatProvider* ), and
   Byte::ToString( String*, IFormatProvider* )
generates the following output when formatting Byte values
with combinations of format strings and IFormatProvider.

IFormatProvider is not used:
   No format string:           13       234
   'X2' format string:         0D        EA

A NumberFormatInfo object with digit group size = 1 and
digit separator = '_' is used for the IFormatProvider:
   No format string:           13       234
   'N' format string:         1_3     2_3_4
*/

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

Byte 構造体 | Byte メンバ | System 名前空間 | Byte.ToString オーバーロードの一覧 | 書式設定の概要 | Parse | String | NumberFormatInfo | IFormatProvider