SByte.ToString メソッド (String, IFormatProvider)
指定した書式およびカルチャに固有の書式情報を使用して、このインスタンスの数値を、それと等価な文字列形式に変換します。
SByte 型は CLS との互換性がありません。CLS と互換性のある型は、Int16 です。CLS との互換性に関する詳細については 「共通言語仕様の概要」 を参照してください。
Overloads Public Overridable Function ToString( _
ByVal format As String, _ ByVal provider As IFormatProvider _) As String
[C#]
public virtual string ToString(stringformat,IFormatProviderprovider);
[C++]
public: virtual String* ToString(String* format,IFormatProvider* provider);
[JScript]
public function ToString(
format : String,provider : IFormatProvider) : String;
パラメータ
- format
書式指定。 - provider
カルチャ固有の書式情報を提供する IFormatProvider 。
戻り値
format および provider で指定された、このインスタンスの値の文字列形式。
例外
例外の種類 | 条件 |
---|---|
FormatException | format が無効です。 |
解説
format が null 参照 (Visual Basic では Nothing) または空の文字列の場合、このインスタンスの戻り値は、一般書式指定子 ("G") を使用して書式設定されます。
provider パラメータは、 NumberFormatInfo オブジェクトを取得する IFormatProvider です。 NumberFormatInfo は、このインスタンスに関するカルチャ固有の書式情報を提供します。 provider が null 参照 (Nothing) の場合、このインスタンスの戻り値は、現在のカルチャの NumberFormatInfo を使用して書式設定されます。
使用例
[Visual Basic, C#, C++] ToString メソッドの複数のオーバーロードを使用して SByte (符号付きバイト) 値を書式化するコード例を次に示します。
' Example for the SByte.ToString( ) methods.
Imports System
Imports System.Globalization
Imports Microsoft.VisualBasic
Module SByteToStringDemo
Sub RunToStringDemo( )
Dim smallValue As SByte = Convert.ToSByte( -99 )
Dim largeValue As SByte = Convert.ToSByte( 123 )
' Format the SByte 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 decimal digits to 0. Set the negative pattern to ( ).
numInfo.NumberNegativePattern = 0
numInfo.NumberDecimalDigits = 0
' Use the NumberFormatInfo object for an IFormatProvider.
Console.WriteLine( vbCrLf & _
"A NumberFormatInfo object with negative pattern " & _
"= ( ) and " & vbCrLf & "no decimal digits " & _
"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 & _
" SByte.ToString( )," & vbCrLf & _
" SByte.ToString( String )," & vbCrLf & _
" SByte.ToString( IFormatProvider ), and" & vbCrLf & _
" SByte.ToString( String, IFormatProvider )" & _
vbCrLf & "generates the following output when " & _
"formatting SByte values " & vbCrLf & _
"with combinations of format " & _
"strings and IFormatProvider." )
RunToStringDemo( )
End Sub
End Module
' This example of
' SByte.ToString( ),
' SByte.ToString( String ),
' SByte.ToString( IFormatProvider ), and
' SByte.ToString( String, IFormatProvider )
' generates the following output when formatting SByte values
' with combinations of format strings and IFormatProvider.
'
' IFormatProvider is not used:
' No format string: -99 123
' 'X2' format string: 9D 7B
'
' A NumberFormatInfo object with negative pattern = ( ) and
' no decimal digits is used for the IFormatProvider:
' No format string: -99 123
' 'N' format string: (99) 123
[C#]
// Example for the SByte.ToString( ) methods.
using System;
using System.Globalization;
public class SByteToStringDemo
{
static void RunToStringDemo( )
{
SByte smallValue = -99;
SByte largeValue = 123;
// Format the SByte 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 decimal digits to 0. Set the negative pattern to ( ).
numInfo.NumberDecimalDigits = 0;
numInfo.NumberNegativePattern = 0;
// Use the NumberFormatInfo object for an IFormatProvider.
Console.WriteLine( "\nA NumberFormatInfo " +
"object with negative pattern = ( ) and \nno " +
"decimal digits 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 SByte.ToString( ),\n" +
" SByte.ToString( string ),\n" +
" SByte.ToString( IFormatProvider ), and\n" +
" SByte.ToString( string, IFormatProvider )\n" +
"generates the following output when formatting " +
"SByte values \nwith combinations of format " +
"strings and IFormatProvider." );
RunToStringDemo( );
}
}
/*
This example of
SByte.ToString( ),
SByte.ToString( string ),
SByte.ToString( IFormatProvider ), and
SByte.ToString( string, IFormatProvider )
generates the following output when formatting SByte values
with combinations of format strings and IFormatProvider.
IFormatProvider is not used:
No format string: -99 123
'X2' format string: 9D 7B
A NumberFormatInfo object with negative pattern = ( ) and
no decimal digits is used for the IFormatProvider:
No format string: -99 123
'N' format string: (99) 123
*/
[C++]
// Example for the SByte::ToString( ) methods.
#using <mscorlib.dll>
using namespace System;
using namespace System::Globalization;
void RunToStringDemo( )
{
SByte smallValue = -99;
SByte largeValue = 123;
// 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 decimal digits to 0. Set the negative pattern to ( ).
numInfo->NumberDecimalDigits = 0;
numInfo->NumberNegativePattern = 0;
// Use the NumberFormatInfo object for an IFormatProvider.
Console::WriteLine( S"\nA NumberFormatInfo "
S"object with negative pattern = ( ) and \nno "
S"decimal digits 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 SByte::ToString( ),\n"
S" SByte::ToString( String* ),\n"
S" SByte::ToString( IFormatProvider* ), and\n"
S" SByte::ToString( String*, IFormatProvider* )\n"
S"generates the following output when formatting "
S"SByte values \nwith combinations of format "
S"strings and IFormatProvider." );
RunToStringDemo( );
}
/*
This example of
SByte::ToString( ),
SByte::ToString( String* ),
SByte::ToString( IFormatProvider* ), and
SByte::ToString( String*, IFormatProvider* )
generates the following output when formatting SByte values
with combinations of format strings and IFormatProvider.
IFormatProvider is not used:
No format string: -99 123
'X2' format string: 9D 7B
A NumberFormatInfo object with negative pattern = ( ) and
no decimal digits is used for the IFormatProvider:
No format string: -99 123
'N' format string: (99) 123
*/
[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
参照
SByte 構造体 | SByte メンバ | System 名前空間 | SByte.ToString オーバーロードの一覧 | 書式設定の概要 | 数値書式指定文字列 | Parse | String