C#程序将浮点数转换为二进制
假设以下是我们的浮点数-
float n = 50.5f;
拿一个空字符串显示二进制值并循环,直到我们的float变量的值大于1-
string a = ""; while (n >= 1) { a = (n % 2) + a; n = n / 2; }
让我们看完整的例子-
示例
using System; using System.IO; using System.CodeDom.Compiler; namespace Program { class Demo { static void Main(string[] args) { //浮动到二进制 Console.WriteLine("float to binary = "); float n = 50.5f; string a = ""; while (n >= 1) { a = (n % 2) + a; n = n / 2; } Console.Write(a); } } }
输出结果
float to binary = 1.5781251.156250.31250.6251.250.5