Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Single.ToString Method in C#
The Single.ToString() method in C# converts a float (single-precision floating-point) value to its string representation. This method is essential for displaying numeric values as text or preparing them for output operations.
Syntax
Following is the syntax for the parameterless ToString() method −
public override string ToString();
The method can also accept format parameters −
public string ToString(string format); public string ToString(IFormatProvider provider); public string ToString(string format, IFormatProvider provider);
Return Value
The method returns a string representation of the float value. Special values like infinity and NaN have specific string representations.
Using ToString() with Regular Values
Example
using System;
public class Demo {
public static void Main() {
float f1 = 123.456f;
float f2 = -67.89f;
float f3 = 0.0f;
Console.WriteLine("Float value: " + f1);
Console.WriteLine("String representation: " + f1.ToString());
Console.WriteLine("Negative value: " + f2.ToString());
Console.WriteLine("Zero value: " + f3.ToString());
}
}
The output of the above code is −
Float value: 123.456 String representation: 123.456 Negative value: -67.89 Zero value: 0
Using ToString() with Special Values
Example
using System;
public class Demo {
public static void Main() {
float f1 = 10.0f / 0.0f; // Positive Infinity
float f2 = -3.0f; // Regular negative value
Console.WriteLine("Value1 (String representation) = " + f1.ToString());
Console.WriteLine("Hashcode for Value1 = " + f1.GetHashCode());
Console.WriteLine("TypeCode for Value1 = " + f1.GetTypeCode());
Console.WriteLine("Is Value1 positive or negative infinity? = " + Single.IsInfinity(f1));
Console.WriteLine("Is Value1 NaN? = " + Single.IsNaN(f1));
Console.WriteLine("Is Value1 a positive infinity? = " + Single.IsPositiveInfinity(f1));
Console.WriteLine("\nValue2 (String representation) = " + f2.ToString());
Console.WriteLine("Hashcode for Value2 = " + f2.GetHashCode());
Console.WriteLine("TypeCode for Value2 = " + f2.GetTypeCode());
Console.WriteLine("Is Value2 positive or negative infinity? = " + Single.IsInfinity(f2));
Console.WriteLine("Is Value2 NaN? = " + Single.IsNaN(f2));
Console.WriteLine("Is Value2 a positive infinity? = " + Single.IsPositiveInfinity(f2));
}
}
The output of the above code is −
Value1 (String representation) = ? Hashcode for Value1 = 2139095040 TypeCode for Value1 = Single Is Value1 positive or negative infinity? = True Is Value1 NaN? = False Is Value1 a positive infinity? = True Value2 (String representation) = -3 Hashcode for Value2 = -1069547520 TypeCode for Value2 = Single Is Value2 positive or negative infinity? = False Is Value2 NaN? = False Is Value2 a positive infinity? = False
Using ToString() with NaN Values
Example
using System;
public class Demo {
public static void Main() {
float f1 = 5.0f / 0.0f; // Positive Infinity
float f2 = 0.0f / 0.0f; // NaN (Not a Number)
Console.WriteLine("Value1 (String representation) = " + f1.ToString());
Console.WriteLine("Hashcode for Value1 = " + f1.GetHashCode());
Console.WriteLine("TypeCode for Value1 = " + f1.GetTypeCode());
Console.WriteLine("Is Value1 positive or negative infinity? = " + Single.IsInfinity(f1));
Console.WriteLine("Is Value1 NaN? = " + Single.IsNaN(f1));
Console.WriteLine("Is Value1 a negative infinity? = " + Single.IsNegativeInfinity(f1));
Console.WriteLine("\nValue2 (String representation) = " + f2.ToString());
Console.WriteLine("Hashcode for Value2 = " + f2.GetHashCode());
Console.WriteLine("TypeCode for Value2 = " + f2.GetTypeCode());
Console.WriteLine("Is Value2 positive or negative infinity? = " + Single.IsInfinity(f2));
Console.WriteLine("Is Value2 NaN? = " + Single.IsNaN(f2));
Console.WriteLine("Is Value2 a negative infinity? = " + Single.IsNegativeInfinity(f2));
}
}
The output of the above code is −
Value1 (String representation) = ? Hashcode for Value1 = 2139095040 TypeCode for Value1 = Single Is Value1 positive or negative infinity? = True Is Value1 NaN? = False Is Value1 a negative infinity? = False Value2 (String representation) = NaN Hashcode for Value2 = -4194304 TypeCode for Value2 = Single Is Value2 positive or negative infinity? = False Is Value2 NaN? = True Is Value2 a negative infinity? = False
Special String Representations
| Float Value | ToString() Result |
|---|---|
| Regular numbers (e.g., 123.45f) | "123.45" |
| Positive Infinity | "?" |
| Negative Infinity | "-?" |
| NaN (Not a Number) | "NaN" |
Conclusion
The Single.ToString() method provides a straightforward way to convert float values to their string representation. It handles special cases like infinity and NaN with appropriate string outputs, making it essential for displaying floating-point values in applications.
