Decimal.ToString メソッド (IFormatProvider)
指定したカルチャ固有の書式情報を使用して、対象のインスタンスの数値を等価の String 形式に変換します。
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 で指定された、このインスタンスの値の String 形式。
解説
パラメータ provider は、 NumberFormatInfo を取得する IFormatProvider です。 NumberFormatInfo は、このインスタンスに関するカルチャ固有の書式情報を提供します。 provider が null 参照 (Visual Basic では Nothing) の場合、このインスタンスの戻り値は、現在のカルチャの NumberFormatInfo を使用して書式設定されます。
使用例
[Visual Basic, C#, C++] ToString メソッドの複数のオーバーロードを使用して Decimal の数値を String に書式化するコード例を次に示します。
' Example for the Decimal.ToString( ) methods.
Imports System
Imports System.Globalization
Imports Microsoft.VisualBasic
Module DecimalToStringDemo
Sub Main( )
Dim nineBillPlus As Decimal = 9876543210.9876543210D
Console.WriteLine( "This example of" & vbCrLf & _
" Decimal.ToString( ), " & vbCrLf & _
" Decimal.ToString( String )," & vbCrLf & _
" Decimal.ToString( IFormatProvider ), and " & _
vbCrLf & " Decimal.ToString( String, IFormat" & _
"Provider )" & vbCrLf & "generates the following " & _
"output when run in the [{0}] culture." & vbCrLf & _
"Decimal numbers are formatted with various " & _
"combinations " & vbCrLf & _
"of format strings and IFormatProvider.", _
CultureInfo.CurrentCulture.Name )
' Format the number without and with format strings.
Console.WriteLine( vbCrLf & "IFormatProvider is not " & _
"used; the default culture is [{0}]:", _
CultureInfo.CurrentCulture.Name )
Console.WriteLine( " {0,-30}{1}", _
"No format string:", nineBillPlus.ToString( ) )
Console.WriteLine( " {0,-30}{1}", _
"'N' format string:", nineBillPlus.ToString( "N" ) )
Console.WriteLine( " {0,-30}{1}", _
"'N5' format string:", nineBillPlus.ToString( "N5" ) )
' Create a CultureInfo object for another culture. Use
' [Dutch - The Netherlands] unless the current culture
' is Dutch language. In that case use [English - U.S.].
Dim cultureName As String = IIf( _
CultureInfo.CurrentCulture.Name.Substring( 0, 2 ) = _
"nl", "en-US", "nl-NL" )
Dim culture As New CultureInfo( cultureName )
' Use the CultureInfo object for an IFormatProvider.
Console.WriteLine( vbCrLf & "A CultureInfo object " & _
"for [{0}] is used for the IFormatProvider: ", _
cultureName )
Console.WriteLine( " {0,-30}{1}", "No format string:", _
nineBillPlus.ToString( culture ) )
Console.WriteLine( " {0,-30}{1}", _
"'N5' format string:", _
nineBillPlus.ToString( "N5", culture ) )
' Get the NumberFormatInfo object from CultureInfo, and
' then change the digit group size to 4 and the digit
' separator to '_'.
Dim numInfo As NumberFormatInfo = culture.NumberFormat
numInfo.NumberGroupSizes = New Integer( ) { 4 }
numInfo.NumberGroupSeparator = "_"
' Use a NumberFormatInfo object for IFormatProvider.
Console.WriteLine( vbCrLf & _
"A NumberFormatInfo object with digit group " & _
"size = 4 and " & vbCrLf & "digit separator " & _
"= '_' is used for the IFormatProvider:" )
Console.WriteLine( " {0,-30}{1}", _
"'N5' format string:", _
nineBillPlus.ToString( "N5", culture ) )
End Sub
End Module
' This example of
' Decimal.ToString( ),
' Decimal.ToString( String ),
' Decimal.ToString( IFormatProvider ), and
' Decimal.ToString( String, IFormatProvider )
' generates the following output when run in the [en-US] culture.
' Decimal numbers are formatted with various combinations
' of format strings and IFormatProvider.
'
' IFormatProvider is not used; the default culture is [en-US]:
' No format string: 9876543210.987654321
' 'N' format string: 9,876,543,210.99
' 'N5' format string: 9,876,543,210.98765
'
' A CultureInfo object for [nl-NL] is used for the IFormatProvider:
' No format string: 9876543210,987654321
' 'N5' format string: 9.876.543.210,98765
'
' A NumberFormatInfo object with digit group size = 4 and
' digit separator = '_' is used for the IFormatProvider:
' 'N5' format string: 98_7654_3210,98765
[C#]
// Example for the Decimal.ToString( ) methods.
using System;
using System.Globalization;
using Microsoft.VisualBasic;
class DecimalToStringDemo
{
static void Main( )
{
decimal nineBillPlus = 9876543210.9876543210M;
Console.WriteLine( "This example of\n" +
" Decimal.ToString( ), \n" +
" Decimal.ToString( String ),\n" +
" Decimal.ToString( IFormatProvider ), and \n" +
" Decimal.ToString( String, IFormatProvider )\n" +
"generates the following output when run in the " +
"[{0}] culture.\nDecimal numbers are formatted " +
"with various combinations \nof format strings " +
"and IFormatProvider.",
CultureInfo.CurrentCulture.Name );
// Format the number without and with format strings.
Console.WriteLine( "\nIFormatProvider is not " +
"used; the default culture is [{0}]:",
CultureInfo.CurrentCulture.Name );
Console.WriteLine( " {0,-30}{1}", "No format string:",
nineBillPlus.ToString( ) );
Console.WriteLine( " {0,-30}{1}", "'N' format string:",
nineBillPlus.ToString( "N" ) );
Console.WriteLine( " {0,-30}{1}", "'N5' format string:",
nineBillPlus.ToString( "N5" ) );
// Create a CultureInfo object for another culture. Use
// [Dutch - The Netherlands] unless the current culture
// is Dutch language. In that case use [English - U.S.].
string cultureName =
CultureInfo.CurrentCulture.Name.Substring( 0, 2 ) ==
"nl" ? "en-US" : "nl-NL";
CultureInfo culture = new CultureInfo( cultureName );
// Use the CultureInfo object for an IFormatProvider.
Console.WriteLine( "\nA CultureInfo object " +
"for [{0}] is used for the IFormatProvider: ",
cultureName );
Console.WriteLine( " {0,-30}{1}", "No format string:",
nineBillPlus.ToString( culture ) );
Console.WriteLine( " {0,-30}{1}", "'N5' format string:",
nineBillPlus.ToString( "N5", culture ) );
// Get the NumberFormatInfo object from CultureInfo, and
// then change the digit group size to 4 and the digit
// separator to '_'.
NumberFormatInfo numInfo = culture.NumberFormat;
numInfo.NumberGroupSizes = new int[ ] { 4 };
numInfo.NumberGroupSeparator = "_";
// Use a NumberFormatInfo object for IFormatProvider.
Console.WriteLine(
"\nA NumberFormatInfo object with digit group " +
"size = 4 and \ndigit separator " +
"= '_' is used for the IFormatProvider:" );
Console.WriteLine( " {0,-30}{1}", "'N5' format string:",
nineBillPlus.ToString( "N5", culture ) );
}
}
/*
This example of
Decimal.ToString( ),
Decimal.ToString( String ),
Decimal.ToString( IFormatProvider ), and
Decimal.ToString( String, IFormatProvider )
generates the following output when run in the [en-US] culture.
Decimal numbers are formatted with various combinations
of format strings and IFormatProvider.
IFormatProvider is not used; the default culture is [en-US]:
No format string: 9876543210.9876543210
'N' format string: 9,876,543,210.99
'N5' format string: 9,876,543,210.98765
A CultureInfo object for [nl-NL] is used for the IFormatProvider:
No format string: 9876543210,9876543210
'N5' format string: 9.876.543.210,98765
A NumberFormatInfo object with digit group size = 4 and
digit separator = '_' is used for the IFormatProvider:
'N5' format string: 98_7654_3210,98765
*/
[C++]
// Sample for the Decimal::ToString( ) methods.
#using <mscorlib.dll>
using namespace System;
using namespace System::Globalization;
using namespace System::Collections;
void main( )
{
Decimal nineBillPlus =
Convert::ToDecimal( S"9876543210.9876543210" );
Console::WriteLine( S"This example of\n"
S" Decimal::ToString( ), \n"
S" Decimal::ToString( String* ),\n"
S" Decimal::ToString( IFormatProvider* ), and \n"
S" Decimal::ToString( String*, IFormatProvider* )\n"
S"generates the following output when run in the "
S"[{0}] culture.\nDecimal numbers are formatted "
S"with various combinations \nof format strings "
S"and IFormatProvider.",
CultureInfo::CurrentCulture->Name );
// Format the number without and with format strings.
Console::WriteLine( S"\nIFormatProvider is not "
S"used; the default culture is [{0}]:",
CultureInfo::CurrentCulture->Name );
Console::WriteLine( S" {0,-30}{1}", S"No format string:",
nineBillPlus.ToString( ) );
Console::WriteLine( S" {0,-30}{1}", S"'N' format string:",
nineBillPlus.ToString( S"N" ) );
Console::WriteLine( S" {0,-30}{1}", S"'N5' format string:",
nineBillPlus.ToString( S"N5" ) );
// Create a CultureInfo object for another culture. Use
// [Dutch - The Netherlands] unless the current culture
// is Dutch language. In that case use [English - U.S.].
String* cultureName =
CultureInfo::CurrentCulture->Name->Substring( 0, 2 ) ==
S"nl" ? S"en-US" : S"nl-NL";
CultureInfo* culture = new CultureInfo( cultureName );
// Use the CultureInfo object for an IFormatProvider.
Console::WriteLine( S"\nA CultureInfo object "
S"for [{0}] is used for the IFormatProvider: ",
cultureName );
Console::WriteLine( S" {0,-30}{1}", S"No format string:",
nineBillPlus.ToString( culture ) );
Console::WriteLine( S" {0,-30}{1}", S"'N5' format string:",
nineBillPlus.ToString( S"N5", culture ) );
// Get the NumberFormatInfo object from CultureInfo, and
// then change the digit group size to 4 and the digit
// separator to '_'.
NumberFormatInfo* numInfo = culture->NumberFormat;
Int32 sizes __gc [] = { 4 };
numInfo->NumberGroupSizes = sizes;
numInfo->NumberGroupSeparator = S"_";
// Use a NumberFormatInfo object for IFormatProvider.
Console::WriteLine(
S"\nA NumberFormatInfo object with digit group "
S"size = 4 and \ndigit separator "
S"= '_' is used for the IFormatProvider:" );
Console::WriteLine( S" {0,-30}{1}", S"'N5' format string:",
nineBillPlus.ToString( S"N5", culture ) );
}
/*
This example of
Decimal::ToString( ),
Decimal::ToString( String* ),
Decimal::ToString( IFormatProvider* ), and
Decimal::ToString( String*, IFormatProvider* )
generates the following output when run in the [en-US] culture.
Decimal numbers are formatted with various combinations
of format strings and IFormatProvider.
IFormatProvider is not used; the default culture is [en-US]:
No format string: 9876543210.9876543210
'N' format string: 9,876,543,210.99
'N5' format string: 9,876,543,210.98765
A CultureInfo object for [nl-NL] is used for the IFormatProvider:
No format string: 9876543210,9876543210
'N5' format string: 9.876.543.210,98765
A NumberFormatInfo object with digit group size = 4 and
digit separator = '_' is used for the IFormatProvider:
'N5' format string: 98_7654_3210,98765
*/
[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
参照
Decimal 構造体 | Decimal メンバ | System 名前空間 | Decimal.ToString オーバーロードの一覧 | 書式設定の概要 | Parse