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

更新於: 2020 年 6 月 22 日

622 次瀏覽

開啟您的 職業

完成課程以獲得認證

開始
廣告
© . All rights reserved.