Let’s say the following is our float −
float n = 50.5f;
Take an empty string to display the binary value and loop until the value of our float variable is greater than 1 −
string a = "";
while (n >= 1) {
a = (n % 2) + a;
n = n / 2;
}Let us see the complete example −
Example
using System;
using System.IO;
using System.CodeDom.Compiler;
namespace Program {
class Demo {
static void Main(string[] args) {
// float to binary
Console.WriteLine("float to binary = ");
float n = 50.5f;
string a = "";
while (n >= 1) {
a = (n % 2) + a;
n = n / 2;
}
Console.Write(a);
}
}
}Output
float to binary = 1.5781251.156250.31250.6251.250.5