Convert.ToSingle Method in C#

The Convert.ToSingle() method in C# converts a specified value to a single-precision floating-point number (float). This method can convert various data types including boolean, integer, string, and other numeric types to a 32-bit floating-point representation.

Syntax

Following is the syntax for the Convert.ToSingle() method −

public static float ToSingle(object value);
public static float ToSingle(bool value);
public static float ToSingle(int value);
public static float ToSingle(string value);

Parameters

  • value − The value to be converted to a single-precision floating-point number. Can be of various types including bool, int, string, double, decimal, etc.

Return Value

Returns a single-precision floating-point number (float) equivalent to the input value. For boolean values, true returns 1.0 and false returns 0.0.

Converting Boolean to Single

When converting boolean values, false becomes 0.0 and true becomes 1.0 −

using System;

public class Demo {
   public static void Main() {
      bool boolVal1 = false;
      bool boolVal2 = true;
      
      float floatVal1 = Convert.ToSingle(boolVal1);
      float floatVal2 = Convert.ToSingle(boolVal2);
      
      Console.WriteLine("Converted {0} to {1}", boolVal1, floatVal1);
      Console.WriteLine("Converted {0} to {1}", boolVal2, floatVal2);
   }
}

The output of the above code is −

Converted False to 0
Converted True to 1

Converting Integer to Single

Integer values are converted directly to their floating-point equivalent −

using System;

public class Demo {
   public static void Main() {
      int intVal = 42;
      long longVal = 1000000L;
      
      float floatVal1 = Convert.ToSingle(intVal);
      float floatVal2 = Convert.ToSingle(longVal);
      
      Console.WriteLine("Converted {0} to {1}", intVal, floatVal1);
      Console.WriteLine("Converted {0} to {1}", longVal, floatVal2);
   }
}

The output of the above code is −

Converted 42 to 42
Converted 1000000 to 1000000

Converting String to Single

String representations of numbers can be converted to single-precision floating-point values −

using System;

public class Demo {
   public static void Main() {
      string strVal1 = "123.45";
      string strVal2 = "99.9";
      
      try {
         float floatVal1 = Convert.ToSingle(strVal1);
         float floatVal2 = Convert.ToSingle(strVal2);
         
         Console.WriteLine("Converted '{0}' to {1}", strVal1, floatVal1);
         Console.WriteLine("Converted '{0}' to {1}", strVal2, floatVal2);
      } catch (FormatException ex) {
         Console.WriteLine("Invalid format: " + ex.Message);
      }
   }
}

The output of the above code is −

Converted '123.45' to 123.45
Converted '99.9' to 99.9

Common Use Cases

Input Type Conversion Result Example
Boolean false ? 0.0, true ? 1.0 Convert.ToSingle(true) returns 1.0
Integer Direct conversion Convert.ToSingle(25) returns 25.0
String Parses numeric string Convert.ToSingle("3.14") returns 3.14
Double May lose precision Convert.ToSingle(3.14159265359) returns 3.141593

Conclusion

The Convert.ToSingle() method provides a flexible way to convert various data types to single-precision floating-point numbers. It handles boolean, integer, string, and other numeric conversions, making it useful for data type conversions in C# applications where float precision is sufficient.

Updated on: 2026-03-17T07:04:35+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements