C# 程序将浮点数转换成二进制数
假设下列内容为我们的浮点数 −
float n = 50.5f;
获取一个空字符串来显示二进制值并循环,直到我们的浮点变量的值大于 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) { // 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); } } }
输出
float to binary = 1.5781251.156250.31250.6251.250.5
广告