SByte.ToString 메서드 (IFormatProvider)
이 메서드는 CLS 규격이 아닙니다.
네임스페이스: System
어셈블리: mscorlib(mscorlib.dll)
public final String ToString ( IFormatProvider provider )
public final function ToString ( provider : IFormatProvider ) : String
적용할 수 없음.
매개 변수
- provider
culture별 형식 지정 정보를 제공하는 IFormatProvider입니다.
반환 값
provider로 지정된 이 인스턴스 값의 문자열 표현입니다.이 인스턴스의 형식은 일반 숫자 형식 지정자("G")를 사용하여 지정됩니다.
.NET Framework에서는 광범위한 형식 지정 기능을 지원합니다. 이러한 지원에 대한 자세한 내용은 다음 형식 지정 항목을 참조하십시오.
숫자 형식 지정자에 대한 자세한 내용은 표준 숫자 형식 문자열 및 사용자 지정 숫자 형식 문자열을 참조하십시오.
provider 매개 변수는 NumberFormatInfo를 가져오는 IFormatProvider입니다. NumberFormatInfo는 이 인스턴스에 대한 culture별 형식 정보를 제공합니다. provider가 null 참조(Visual Basic의 경우 Nothing)인 경우 이 인스턴스의 형식은 현재 culture의 NumberFormatInfo를 사용하여 결정됩니다.
다음 코드 예제에서는 현재 스레드 culture, 지정된 culture 및 모든 표준 숫자 형식 지정자를 사용하여 정수 숫자 값과 부동 소수점 숫자 값의 형식을 지정합니다. 이 코드 예제에서는 두 개의 특정 숫자 형식을 사용하지만 다른 기타 숫자 기본 형식(Byte, SByte, Int16, Int32, Int64, UInt16, UInt32, UInt64, Decimal,Single, Double)을 사용해도 유사한 결과가 생성됩니다.
// This code example demonstrates the ToString(String) and // ToString(String, IFormatProvider) methods for integral and // floating-point numbers, in conjunction with the standard // numeric format specifiers. // This code example uses the System.Int32 integral type and // the System.Double floating-point type, but would yield // similar results for any of the numeric types. The integral // numeric types are System.Byte, SByte, Int16, Int32, Int64, // UInt16, UInt32, and UInt64. The floating-point numeric types // are Decimal, Single, and Double. using System; using System.Globalization; using System.Threading; class Sample { public static void Main() { // Format a negative integer or floating-point number in various ways. int integralVal = -12345; double floatingVal = -1234.567d; string msgCurrency = "(C) Currency: . . . . . . "; string msgDecimal = "(D) Decimal:. . . . . . . "; string msgScientific = "(E) Scientific: . . . . . "; string msgFixedPoint = "(F) Fixed point:. . . . . "; string msgGeneral = "(G) General (default):. . "; string msgNumber = "(N) Number: . . . . . . . "; string msgPercent = "(P) Percent:. . . . . . . "; string msgRoundTrip = "(R) Round-trip: . . . . . "; string msgHexadecimal = "(X) Hexadecimal:. . . . . "; string msg1 = "Use ToString(String) and the current thread culture.\n"; string msg2 = "Use ToString(String, IFormatProvider) and a specified culture.\n"; string msgCulture = "Culture:"; string msgIntegralVal = "Integral value:"; string msgFloatingVal = "Floating-point value:"; CultureInfo ci; // Console.Clear(); Console.WriteLine("Standard Numeric Format Specifiers:\n"); // Display the values. Console.WriteLine(msg1); // Display the thread current culture, which is used to format the values. ci = Thread.CurrentThread.CurrentCulture; Console.WriteLine("{0,-26}{1}", msgCulture, ci.DisplayName); // Display the integral and floating-point values. Console.WriteLine("{0,-26}{1}", msgIntegralVal, integralVal); Console.WriteLine("{0,-26}{1}", msgFloatingVal, floatingVal); Console.WriteLine(); // Use the format specifiers that are only for integral types. Console.WriteLine("Format specifiers only for integral types:"); Console.WriteLine(msgDecimal + integralVal.ToString("D")); Console.WriteLine(msgHexadecimal + integralVal.ToString("X")); Console.WriteLine(); // Use the format specifier that is only for the Single and Double // floating-point types. Console.WriteLine("Format specifier only for the Single and Double types:"); Console.WriteLine(msgRoundTrip + floatingVal.ToString("R")); Console.WriteLine(); // Use the format specifiers that are for integral or floating-point types. Console.WriteLine("Format specifiers for integral or floating-point types:"); Console.WriteLine(msgCurrency + floatingVal.ToString("C")); Console.WriteLine(msgScientific + floatingVal.ToString("E")); Console.WriteLine(msgFixedPoint + floatingVal.ToString("F")); Console.WriteLine(msgGeneral + floatingVal.ToString("G")); Console.WriteLine(msgNumber + floatingVal.ToString("N")); Console.WriteLine(msgPercent + floatingVal.ToString("P")); Console.WriteLine(); // Display the same values using a CultureInfo object. The CultureInfo class // implements IFormatProvider. Console.WriteLine(msg2); // Display the culture used to format the values. // Create a European culture and change its currency symbol to "euro" because // this particular code example uses a thread current UI culture that cannot // display the euro symbol (€). ci = new CultureInfo("de-DE"); ci.NumberFormat.CurrencySymbol = "euro"; Console.WriteLine("{0,-26}{1}", msgCulture, ci.DisplayName); // Display the integral and floating-point values. Console.WriteLine("{0,-26}{1}", msgIntegralVal, integralVal); Console.WriteLine("{0,-26}{1}", msgFloatingVal, floatingVal); Console.WriteLine(); // Use the format specifiers that are only for integral types. Console.WriteLine("Format specifiers only for integral types:"); Console.WriteLine(msgDecimal + integralVal.ToString("D", ci)); Console.WriteLine(msgHexadecimal + integralVal.ToString("X", ci)); Console.WriteLine(); // Use the format specifier that is only for the Single and Double // floating-point types. Console.WriteLine("Format specifier only for the Single and Double types:"); Console.WriteLine(msgRoundTrip + floatingVal.ToString("R", ci)); Console.WriteLine(); // Use the format specifiers that are for integral or floating-point types. Console.WriteLine("Format specifiers for integral or floating-point types:"); Console.WriteLine(msgCurrency + floatingVal.ToString("C", ci)); Console.WriteLine(msgScientific + floatingVal.ToString("E", ci)); Console.WriteLine(msgFixedPoint + floatingVal.ToString("F", ci)); Console.WriteLine(msgGeneral + floatingVal.ToString("G", ci)); Console.WriteLine(msgNumber + floatingVal.ToString("N", ci)); Console.WriteLine(msgPercent + floatingVal.ToString("P", ci)); Console.WriteLine(); } } /* This code example produces the following results: Standard Numeric Format Specifiers: Use ToString(String) and the current thread culture. Culture: English (United States) Integral value: -12345 Floating-point value: -1234.567 Format specifiers only for integral types: (D) Decimal:. . . . . . . -12345 (X) Hexadecimal:. . . . . FFFFCFC7 Format specifier only for the Single and Double types: (R) Round-trip: . . . . . -1234.567 Format specifiers for integral or floating-point types: (C) Currency: . . . . . . ($1,234.57) (E) Scientific: . . . . . -1.234567E+003 (F) Fixed point:. . . . . -1234.57 (G) General (default):. . -1234.567 (N) Number: . . . . . . . -1,234.57 (P) Percent:. . . . . . . -123,456.70 % Use ToString(String, IFormatProvider) and a specified culture. Culture: German (Germany) Integral value: -12345 Floating-point value: -1234.567 Format specifiers only for integral types: (D) Decimal:. . . . . . . -12345 (X) Hexadecimal:. . . . . FFFFCFC7 Format specifier only for the Single and Double types: (R) Round-trip: . . . . . -1234,567 Format specifiers for integral or floating-point types: (C) Currency: . . . . . . -1.234,57 euro (E) Scientific: . . . . . -1,234567E+003 (F) Fixed point:. . . . . -1234,57 (G) General (default):. . -1234,567 (N) Number: . . . . . . . -1.234,57 (P) Percent:. . . . . . . -123.456,70% */
다음 코드 예제에서는 ToString 메서드의 몇 가지 오버로드를 사용하여 SByte(부호 있는 바이트) 값의 형식을 지정합니다.
// 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 */
// Example for the SByte.ToString( ) methods. import System.*; import System.Globalization.*; public class SByteToStringDemo { static void RunToStringDemo() { SByte smallValue = (SByte)SByte.Parse(System.Convert.ToString(-99)); SByte largeValue = (SByte)SByte.Parse(System.Convert.ToString(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.get_NumberFormat(); // Set decimal digits to 0. Set the negative pattern to ( ). numInfo.set_NumberDecimalDigits(0); numInfo.set_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)); } //RunToStringDemo public static void main(String[] args) { 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(); } //main } //SByteToStringDemo /* 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 */
Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
Microsoft .NET Framework 3.0은 Windows Vista, Microsoft Windows XP SP2 및 Windows Server 2003 SP1에서 지원됩니다.
출처: MSDN
'Language > C#' 카테고리의 다른 글
Winform에서 키 이벤트 안먹음 (0) | 2016.02.04 |
---|---|
ini파일 읽고 쓰기 (0) | 2016.02.04 |
WPF 예제 샘플 (0) | 2016.02.04 |
[C#] PrintDocument 이용한 다중 페이지 출력 (0) | 2016.02.04 |
VisualStudio 단축키 (0) | 2016.02.04 |