Export (0) Print
Expand All

SByte.ToString Method (String)

Converts the numeric value of this instance to its equivalent string representation, using the specified format.

This method is not CLS-compliant.  

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

public string ToString (
	string format
)
public String ToString (
	String format
)
public function ToString (
	format : String
) : String

Parameters

format

A format string.

Return Value

The string representation of the value of this instance as specified by format.

Exception typeCondition

FormatException

format is invalid.

If format is a null reference (Nothing in Visual Basic) or an empty string, the return value of this instance is formatted with the general format specifier ("G").

The return value of this instance is formatted with the NumberFormatInfo for the current culture.

The following code example formats SByte (signed byte) values with several overloads of the ToString method.

// 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 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

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft