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) {

         // 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.