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
C# program to convert floating to binary
Converting a floating-point number to binary representation in C# involves separating the number into its integer and fractional parts, then converting each part separately using different algorithms.
The integer part is converted using repeated division by 2, while the fractional part is converted using repeated multiplication by 2.
Algorithm
The conversion process follows these steps −
Separate the floating-point number into integer and fractional parts
Convert the integer part by repeatedly dividing by 2 and collecting remainders
Convert the fractional part by repeatedly multiplying by 2 and collecting integer parts
Combine both parts with a binary point
Using Integer and Fractional Part Separation
Example
using System;
class Program {
static void Main() {
float number = 50.5f;
Console.WriteLine("Converting " + number + " to binary:");
string binaryResult = FloatToBinary(number);
Console.WriteLine("Binary representation: " + binaryResult);
}
static string FloatToBinary(float number) {
if (number == 0) return "0";
// Separate integer and fractional parts
int integerPart = (int)number;
float fractionalPart = number - integerPart;
// Convert integer part
string integerBinary = IntegerToBinary(integerPart);
// Convert fractional part
string fractionalBinary = FractionalToBinary(fractionalPart);
return integerBinary + "." + fractionalBinary;
}
static string IntegerToBinary(int number) {
if (number == 0) return "0";
string binary = "";
while (number > 0) {
binary = (number % 2) + binary;
number = number / 2;
}
return binary;
}
static string FractionalToBinary(float fraction) {
string binary = "";
int precision = 10; // Limit precision to avoid infinite loop
while (fraction > 0 && precision > 0) {
fraction *= 2;
if (fraction >= 1) {
binary += "1";
fraction -= 1;
} else {
binary += "0";
}
precision--;
}
return binary.Length > 0 ? binary : "0";
}
}
The output of the above code is −
Converting 50.5 to binary: Binary representation: 110010.1
Using BitConverter for IEEE 754 Representation
For the actual IEEE 754 binary representation used internally by computers −
Example
using System;
class Program {
static void Main() {
float number = 50.5f;
Console.WriteLine("Float value: " + number);
// Get IEEE 754 binary representation
byte[] bytes = BitConverter.GetBytes(number);
string binaryString = "";
// Convert each byte to binary (reverse order for little-endian)
for (int i = bytes.Length - 1; i >= 0; i--) {
binaryString += Convert.ToString(bytes[i], 2).PadLeft(8, '0');
}
Console.WriteLine("IEEE 754 binary: " + binaryString);
// Break down the IEEE 754 format
Console.WriteLine("Sign bit: " + binaryString[0]);
Console.WriteLine("Exponent: " + binaryString.Substring(1, 8));
Console.WriteLine("Mantissa: " + binaryString.Substring(9));
}
}
The output of the above code is −
Float value: 50.5 IEEE 754 binary: 01000010010010010000000000000000 Sign bit: 0 Exponent: 10000100 Mantissa: 10010010000000000000000
Handling Different Float Values
Example
using System;
class Program {
static void Main() {
float[] numbers = { 12.75f, 0.375f, 100.25f };
foreach (float num in numbers) {
Console.WriteLine(num + " ? " + FloatToBinary(num));
}
}
static string FloatToBinary(float number) {
if (number == 0) return "0";
int integerPart = (int)number;
float fractionalPart = number - integerPart;
string integerBinary = IntegerToBinary(integerPart);
string fractionalBinary = FractionalToBinary(fractionalPart);
return integerBinary + "." + fractionalBinary;
}
static string IntegerToBinary(int number) {
if (number == 0) return "0";
string binary = "";
while (number > 0) {
binary = (number % 2) + binary;
number /= 2;
}
return binary;
}
static string FractionalToBinary(float fraction) {
string binary = "";
int precision = 8;
while (fraction > 0 && precision > 0) {
fraction *= 2;
if (fraction >= 1) {
binary += "1";
fraction -= 1;
} else {
binary += "0";
}
precision--;
}
return binary.Length > 0 ? binary : "0";
}
}
The output of the above code is −
12.75 ? 1100.11 0.375 ? 0.011 100.25 ? 1100100.01
Conclusion
Converting floating-point numbers to binary requires handling integer and fractional parts separately. The integer part uses division by 2, while the fractional part uses multiplication by 2. For precise IEEE 754 representation, use BitConverter.GetBytes() to get the actual binary format used by the computer.
