Single.ToString() Method in C# | Set – 2

Single.ToString() Method is used to convert the numeric value of the current instance to its equivalent string representation. There are 4 methods in the overload list of this method as follows:

  • ToString(String) Method
  • ToString() Method
  • ToString(IFormatProvider) Method
  • ToString(String, IFormatProvider) Method

Here, we will discuss the last two methods.

ToString(IFormatProvider) Method

This method is used to convert the numeric value of the current instance to its equivalent string representation using the specified culture-specific format information.



Syntax:

public string ToString (IFormatProvider provider);

Parameters: This method takes an object of type IFormatProvider which supplies culture-specific formatting information.

Return Value: This method returns the string representation of the value of the current instance in the format specified by the provider parameter.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate
// Single.ToString(IFormatProvider)
// Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // declaring and initializing
        // Single value
        float s1 = 65465.67867f;
  
        // creating and initializing
        // the object of CultureInfo
        CultureInfo provider = new CultureInfo("en-us");
  
        // Using the method
        string str = s1.ToString(provider);
  
        // Display the value
        Console.WriteLine("The Value is {0} and provider is {1}",
                                            str, provider.Name);
    }
}

chevron_right


Output:

The Value is 65465.68 and provider is en-US

Single.ToString(String, IFormatProvider) Method

This method is used to convert the numeric value of this instance to its equivalent string representation using the specified format and culture-specific formatting information.

Syntax:

public string ToString (string format, IFormatProvider provider);

Parameters:

format: It is a standard or custom numeric format string.
provider: It is an object which supplies culture-specific formatting information.

Return Value: This method returns the string representation of the current instance specified by the format and provider parameter.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Single.ToString(String, IFormatProvider)
// Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // declaring and intializing Single value
        float s1 = Single.MaxValue;
  
        // creating and initializing
        // the object of CultureInfo
        CultureInfo provider = new CultureInfo("es-ES");
  
        // declaring and intializing format
        string format = "N";
  
        // using the method
        string str = s1.ToString(format, provider);
  
        // Displaying the details
        Console.WriteLine("The value is {0}", str);
        Console.WriteLine("The Format is {0}", format);
        Console.WriteLine("The Provider is {0}", provider.Name);
    }
}

chevron_right


Output:

The value is 340.282.300.000.000.000.000.000.000.000.000.000.000,00
The Format is N
The Provider is es-ES

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.