Skip to main content

Convert the fractional whole number to binary

Converting a fractional whole number to binary means expressing a non-negative decimal number that has a fractional part as a binary number, which uses only the digits 0 and 1.

The process of converting a fractional decimal number to binary involves multiplying the decimal number by 2 repeatedly, noting the integer part of the result, and using the fractional part as the input for the next multiplication. The binary representation is obtained by concatenating the integer parts obtained at each step.

For example, to convert the decimal number 0.625 to binary:

  1. Multiply 0.625 by 2, the integer part is 1, and the fractional part is 0.25.
  2. Multiply 0.25 by 2, the integer part is 0, and the fractional part is 0.5.
  3. Multiply 0.5 by 2, the integer part is 1, and the fractional part is 0.
  4. The binary representation is obtained by concatenating the integer parts obtained at each step: 0.625 in binary is 0.101.

Therefore, the binary representation of 0.625 is 0.101. Other Example:

Example 1 
----------
num = 12.55

12 = 1100 (Binary of whole number)


0.10000000000000142 X 2 = 0.20000000000000284  0
0.20000000000000284 X 2 = 0.4000000000000057  0
0.4000000000000057 X 2 = 0.8000000000000114  0
0.8000000000000114 X 2 = 1.6000000000000227  1
0.6000000000000227 X 2 = 1.2000000000000455  1
0.20000000000004547 X 2 = 0.40000000000009095  0
0.40000000000009095 X 2 = 0.8000000000001819  0
0.8000000000001819 X 2 = 1.6000000000003638  1
0.6000000000003638 X 2 = 1.2000000000007276  1
0.2000000000007276 X 2 = 0.4000000000014552  0
0.4000000000014552 X 2 = 0.8000000000029104  0
0.8000000000029104 X 2 = 1.6000000000058208  1
0.6000000000058208 X 2 = 1.2000000000116415  1
0.20000000001164153 X 2 = 0.40000000002328306  0
0.40000000002328306 X 2 = 0.8000000000465661  0
0.8000000000465661 X 2 = 1.6000000000931323  1
0.6000000000931323 X 2 = 1.2000000001862645  1
0.20000000018626451 X 2 = 0.40000000037252903  0
0.40000000037252903 X 2 = 0.8000000007450581  0
0.8000000007450581 X 2 = 1.6000000014901161  1
0.6000000014901161 X 2 = 1.2000000029802322  1
0.20000000298023224 X 2 = 0.4000000059604645  0
0.4000000059604645 X 2 = 0.800000011920929  0
0.800000011920929 X 2 = 1.600000023841858  1
0.6000000238418579 X 2 = 1.2000000476837158  1
0.20000004768371582 X 2 = 0.40000009536743164  0
0.40000009536743164 X 2 = 0.8000001907348633  0
0.8000001907348633 X 2 = 1.6000003814697266  1
0.6000003814697266 X 2 = 1.2000007629394531  1
0.20000076293945312 X 2 = 0.40000152587890625  0
0.40000152587890625 X 2 = 0.8000030517578125  0
0.8000030517578125 X 2 = 1.600006103515625  1
0.600006103515625 X 2 = 1.20001220703125  1
0.20001220703125 X 2 = 0.4000244140625  0
0.4000244140625 X 2 = 0.800048828125  0
0.800048828125 X 2 = 1.60009765625  1
0.60009765625 X 2 = 1.2001953125  1
0.2001953125 X 2 = 0.400390625  0
0.400390625 X 2 = 0.80078125  0
0.80078125 X 2 = 1.6015625  1
0.6015625 X 2 = 1.203125  1
0.203125 X 2 = 0.40625  0
0.40625 X 2 = 0.8125  0
0.8125 X 2 = 1.625  1
0.625 X 2 = 1.25  1
0.25 X 2 = 0.5  0
0.5 X 2 = 1.0  1 

// Result
1100.100011001100110011001100110011001100110011001101
or (fractional of 16 bit)
1100.10001100110011001
 
Example 2
-----------
num = 99.14	


99 = 1100011 (Binary of whole number)

Calculate fractional

 
 0.14 X 2 = 0.28  0
 0.28 X 2 = 0.56  0
 0.56 X 2 = 1.12  1
 0.1200000000000001 X 2 = 0.2400000000000002  0
 0.2400000000000002 X 2 = 0.4800000000000004  0
 0.4800000000000004 X 2 = 0.9600000000000009  0
 0.9600000000000009 X 2 = 1.9200000000000017  1
 0.9200000000000017 X 2 = 1.8400000000000034  1
 0.8400000000000034 X 2 = 1.6800000000000068  1
 0.6800000000000068 X 2 = 1.3600000000000136  1
 0.36000000000001364 X 2 = 0.7200000000000273  0
 0.7200000000000273 X 2 = 1.4400000000000546  1
 0.44000000000005457 X 2 = 0.8800000000001091  0
 0.8800000000001091 X 2 = 1.7600000000002183  1
 0.7600000000002183 X 2 = 1.5200000000004366  1
 0.5200000000004366 X 2 = 1.0400000000008731  1
 0.040000000000873115 X 2 = 0.08000000000174623  0
 0.08000000000174623 X 2 = 0.16000000000349246  0
 0.16000000000349246 X 2 = 0.3200000000069849  0
 0.3200000000069849 X 2 = 0.6400000000139698  0
 0.6400000000139698 X 2 = 1.2800000000279397  1
 0.2800000000279397 X 2 = 0.5600000000558794  0
 0.5600000000558794 X 2 = 1.1200000001117587  1
 0.12000000011175871 X 2 = 0.24000000022351742  0
 0.24000000022351742 X 2 = 0.48000000044703484  0
 0.48000000044703484 X 2 = 0.9600000008940697  0
 0.9600000008940697 X 2 = 1.9200000017881393  1
 0.9200000017881393 X 2 = 1.8400000035762787  1
 0.8400000035762787 X 2 = 1.6800000071525574  1
 0.6800000071525574 X 2 = 1.3600000143051147  1
 0.36000001430511475 X 2 = 0.7200000286102295  0
 0.7200000286102295 X 2 = 1.440000057220459  1
 0.440000057220459 X 2 = 0.880000114440918  0
 0.880000114440918 X 2 = 1.760000228881836  1
 0.7600002288818359 X 2 = 1.5200004577636719  1
 0.5200004577636719 X 2 = 1.0400009155273438  1
 0.04000091552734375 X 2 = 0.0800018310546875  0
 0.0800018310546875 X 2 = 0.160003662109375  0
 0.160003662109375 X 2 = 0.32000732421875  0
 0.32000732421875 X 2 = 0.6400146484375  0
 0.6400146484375 X 2 = 1.280029296875  1
 0.280029296875 X 2 = 0.56005859375  0
 0.56005859375 X 2 = 1.1201171875  1
 0.1201171875 X 2 = 0.240234375  0
 0.240234375 X 2 = 0.48046875  0
 0.48046875 X 2 = 0.9609375  0
 0.9609375 X 2 = 1.921875  1
 0.921875 X 2 = 1.84375  1
 0.84375 X 2 = 1.6875  1
 0.6875 X 2 = 1.375  1
 0.375 X 2 = 0.75  0
 0.75 X 2 = 1.5  1
 0.5 X 2 = 1.0  1
 Given Number : 99.14
 Result .00100011110101110000101000111101011100001010001111011
 or 16 bits
 1100011.0010001111010111

Here given code implementation process.

// Java Program 
// Convert the fractional whole number to binary
public class Conversion
{
	public void binaryNo(double num)
	{
		if (num < 0)
		{
			return;
		}
		// Get whole number
		int whole = (int) num;
		// Get approximate fraction part
		double fractional = (num - whole);
		String result = "";
		// Find binary of whole number
		while (whole > 0)
		{
			result = (whole % 2) + result;
			whole /= 2;
		}
		result += ".";
		int bit = 0;
		// Find binary of fractional number
		// Consider it up to 16 digit
		while (fractional > 0 && bit < 16)
		{
			fractional *= 2;
			if ((int) fractional == 1)
			{
				fractional = fractional - 1;
				result += "1";
			}
			else
			{
				result += "0";
			}
			// increase counter of a fraction bits
			bit += 1;
		}
		System.out.print("\n Given Number : " + num);
		// Display calculated result
		System.out.print("\n Result " + result);
	}
	public static void main(String[] args)
	{
		Conversion task = new Conversion();
		// Test Case
		task.binaryNo(12.55);
		task.binaryNo(99.14);
	}
}

Output

 Given Number : 12.55
 Result 1100.1000110011001100
 Given Number : 99.14
 Result 1100011.0010001111010111
// Include header file
#include <iostream>
#include <string>
using namespace std;
// C++ Program
// Convert the fractional whole number to binary
class Conversion
{
	public: void binaryNo(double num)
	{
		if (num < 0)
		{
			return;
		}
		// Get whole number
		int whole = (int) num;
		// Get approximate fraction part
		double fractional = (num - whole);
		string result = "";
		// Find binary of whole number
		while (whole > 0)
		{
			result = to_string(whole % 2) + result;
			whole /= 2;
		}
		result += ".";
		int bit = 0;
		// Find binary of fractional number
		// Consider it up to 16 digit
		while (fractional > 0 && bit < 16)
		{
			fractional *= 2;
			if ((int) fractional == 1)
			{
				fractional = fractional - 1;
				result += "1";
			}
			else
			{
				result += "0";
			}
			// increase counter of a fraction bits
			bit += 1;
		}
		cout << "\n Given Number : " << num;
		// Display calculated result
		cout << "\n Result " << result;
	}
};
int main()
{
	Conversion task = Conversion();
	// Test Case
	task.binaryNo(12.55);
	task.binaryNo(99.14);
	return 0;
}

Output

 Given Number : 12.55
 Result 1100.1000110011001100
 Given Number : 99.14
 Result 1100011.0010001111010111
// Include namespace system
using System;
// C# Program
// Convert the fractional whole number to binary
public class Conversion
{
	public void binaryNo(double num)
	{
		if (num < 0)
		{
			return;
		}
		// Get whole number
		int whole = (int) num;
		// Get approximate fraction part
		double fractional = (num - whole);
		String result = "";
		// Find binary of whole number
		while (whole > 0)
		{
			result = (whole % 2) + result;
			whole /= 2;
		}
		result += ".";
		int bit = 0;
		// Find binary of fractional number
		// Consider it up to 16 digit
		while (fractional > 0 && bit < 16)
		{
			fractional *= 2;
			if ((int) fractional == 1)
			{
				fractional = fractional - 1;
				result += "1";
			}
			else
			{
				result += "0";
			}
			// increase counter of a fraction bits
			bit += 1;
		}
		Console.Write("\n Given Number : " + num);
		// Display calculated result
		Console.Write("\n Result " + result);
	}
	public static void Main(String[] args)
	{
		Conversion task = new Conversion();
		// Test Case
		task.binaryNo(12.55);
		task.binaryNo(99.14);
	}
}

Output

 Given Number : 12.55
 Result 1100.1000110011001100
 Given Number : 99.14
 Result 1100011.0010001111010111
<?php
// Php Program
// Convert the fractional whole number to binary
class Conversion
{
	public	function binaryNo($num)
	{
		if ($num < 0)
		{
			return;
		}
		// Get whole number
		$whole = (int) $num;
		// Get approximate fraction part
		$fractional = ($num - $whole);
		$result = "";
		// Find binary of whole number
		while ($whole > 0)
		{
			$result = ($whole % 2) . $result;
			$whole = intval($whole / 2);
		}
		$result .= ".";
		$bit = 0;
		// Find binary of fractional number
		// Consider it up to 16 digit
		while ($fractional > 0 && $bit < 16)
		{
			$fractional *= 2;
			if ((int) $fractional == 1)
			{
				$fractional = $fractional - 1;
				$result .= "1";
			}
			else
			{
				$result .= "0";
			}
			// increase counter of a fraction bits
			$bit += 1;
		}
		echo "\n Given Number : ". $num;
		// Display calculated result
		echo "\n Result ". $result;
	}
}

function main()
{
	$task = new Conversion();
	$task->binaryNo(12.55);
	$task->binaryNo(99.14);
}
main();

Output

 Given Number : 12.55
 Result 1100.1000110011001100
 Given Number : 99.14
 Result 1100011.0010001111010111
// Node Js Program
// Convert the fractional whole number to binary
class Conversion
{
	binaryNo(num)
	{
		if (num < 0)
		{
			return;
		}
		// Get whole number
		var whole = parseInt(num);
		// Get approximate fraction part
		var fractional = (num - whole);
		var result = "";
		// Find binary of whole number
		while (whole > 0)
		{
			result = (whole % 2) + result;
			whole = parseInt(whole / 2);
		}
		result += ".";
		var bit = 0;
		// Find binary of fractional number
		// Consider it up to 16 digit
		while (fractional > 0 && bit < 16)
		{
			fractional *= 2;
			if (parseInt(fractional) == 1)
			{
				fractional = fractional - 1;
				result += "1";
			}
			else
			{
				result += "0";
			}
			// increase counter of a fraction bits
			bit += 1;
		}
		process.stdout.write("\n Given Number : " + num);
		// Display calculated result
		process.stdout.write("\n Result " + result);
	}
}

function main()
{
	var task = new Conversion();
	// Test Case
	task.binaryNo(12.55);
	task.binaryNo(99.14);
}
main();

Output

 Given Number : 12.55
 Result 1100.1000110011001100
 Given Number : 99.14
 Result 1100011.0010001111010111
#  Python 3 Program 
#  Convert the fractional whole number to binary
class Conversion :
	def binaryNo(self, num) :
		if (num < 0) :
			return
		
		#  Get whole number
		whole = int(num)
		#  Get approximate fraction part
		fractional = (num - whole)
		result = ""
		#  Find binary of whole number
		while (whole > 0) :
			result = str(whole % 2) + result
			whole = int(whole / 2)
		
		result += "."
		bit = 0
		#  Find binary of fractional number
		#  Consider it up to 16 digit
		while (fractional > 0 and bit < 16) :
			fractional *= 2
			if (int(fractional) == 1) :
				fractional = fractional - 1
				result += "1"
			else :
				result += "0"
			
			#  increase counter of a fraction bits
			bit += 1
		
		print("\n Given Number : ", num, end = "")
		#  Display calculated result
		print("\n Result ", result, end = "")
	

def main() :
	task = Conversion()
	#  Test Case
	task.binaryNo(12.55)
	task.binaryNo(99.14)

if __name__ == "__main__": main()

Output

 Given Number :  12.55
 Result  1100.1000110011001100
 Given Number :  99.14
 Result  1100011.0010001111010111
#  Ruby Program 
#  Convert the fractional whole number to binary
class Conversion 
	def binaryNo(num) 
		if (num < 0) 
			return
		end

		#  Get whole number
		whole = (num).to_i
		#  Get approximate fraction part
		fractional = (num - whole)
		result = ""
		#  Find binary of whole number
		while (whole > 0) 
			result = (whole % 2).to_s + result
			whole /= 2
		end

		result += "."
		bit = 0
		#  Find binary of fractional number
		#  Consider it up to 16 digit
		while (fractional > 0 && bit < 16) 
			fractional *= 2
			if ((fractional).to_i == 1) 
				fractional = fractional - 1
				result += "1"
			else 
				result += "0"
			end

			#  increase counter of a fraction bits
			bit += 1
		end

		print("\n Given Number : ", num)
		#  Display calculated result
		print("\n Result ", result)
	end

end

def main() 
	task = Conversion.new()
	#  Test Case
	task.binaryNo(12.55)
	task.binaryNo(99.14)
end

main()

Output

 Given Number : 12.55
 Result 1100.1000110011001100
 Given Number : 99.14
 Result 1100011.0010001111010111
// Scala Program
// Convert the fractional whole number to binary
class Conversion
{
	def binaryNo(num: Double): Unit = {
		if (num < 0)
		{
			return;
		}
		// Get whole number
		var whole: Int = (num).toInt;
		// Get approximate fraction part
		var fractional: Double = (num - whole);
		var result: String = "";
		// Find binary of whole number
		while (whole > 0)
		{
			result = ""+(whole % 2) + result;
			whole = (whole / 2).toInt;
		}
		result += ".";
		var bit: Int = 0;
		// Find binary of fractional number
		// Consider it up to 16 digit
		while (fractional > 0 && bit < 16)
		{
			fractional *= 2;
			if ((fractional).toInt == 1)
			{
				fractional = fractional - 1;
				result += "1";
			}
			else
			{
				result += "0";
			}
			// increase counter of a fraction bits
			bit += 1;
		}
		print("\n Given Number : " + num);
		// Display calculated result
		print("\n Result " + result);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Conversion = new Conversion();
		// Test Case
		task.binaryNo(12.55);
		task.binaryNo(99.14);
	}
}

Output

 Given Number : 12.55
 Result 1100.1000110011001100
 Given Number : 99.14
 Result 1100011.0010001111010111
// Swift 4 Program
// Convert the fractional whole number to binary
class Conversion
{
	func binaryNo(_ num: Double)
	{
		if (num < 0)
		{
			return;
		}
		// Get whole number
		var whole: Int = Int(num);
		// Get approximate fraction part
		var fractional: Double = (num - Double(whole));
		var result: String = "";
		// Find binary of whole number
		while (whole > 0)
		{
			result = String(whole % 2) + result;
			whole /= 2;
		}
		result += ".";
		var bit: Int = 0;
		// Find binary of fractional number
		// Consider it up to 16 digit
		while (fractional > 0 && bit < 16)
		{
			fractional *= 2;
			if (Int(fractional) == 1)
			{
				fractional = fractional - 1;
				result += "1";
			}
			else
			{
				result += "0";
			}
			// increase counter of a fraction bits
			bit += 1;
		}
		print("\n Given Number : ", num, terminator: "");
		// Display calculated result
		print("\n Result ", result, terminator: "");
	}
}
func main()
{
	let task: Conversion = Conversion();
	// Test Case
	task.binaryNo(12.55);
	task.binaryNo(99.14);
}
main();

Output

 Given Number :  12.55
 Result  1100.1000110011001100
 Given Number :  99.14
 Result  1100011.0010001111010111
// Kotlin Program
// Convert the fractional whole number to binary
class Conversion
{
	fun binaryNo(num: Double): Unit
	{
		if (num < 0)
		{
			return;
		}
		// Get whole number
		var whole: Int = num.toInt();
		// Get approximate fraction part
		var fractional: Double = (num - whole.toDouble());
		var result: String = "";
		// Find binary of whole number
		while (whole > 0)
		{
			result = (whole % 2).toString() + result;
			whole /= 2;
		}
		result += ".";
		var bit: Int = 0;
		// Find binary of fractional number
		// Consider it up to 16 digit
		while (fractional > 0 && bit < 16)
		{
			fractional *= 2;
			if (fractional.toInt() == 1)
			{
				fractional = fractional - 1;
				result += "1";
			}
			else
			{
				result += "0";
			}
			// increase counter of a fraction bits
			bit += 1;
		}
		print("\n Given Number : " + num);
		// Display calculated result
		print("\n Result " + result);
	}
}
fun main(args: Array < String > ): Unit
{
	var task: Conversion = Conversion();
	// Test Case
	task.binaryNo(12.55);
	task.binaryNo(99.14);
}

Output

 Given Number : 12.55
 Result 1100.1000110011001100
 Given Number : 99.14
 Result 1100011.0010001111010111




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment