Decimal to binary

I'm having trouble figuring out how to convert a decimal such as 4.050 to binary, octal, and hex.
I'm familiar using modulus, but since you can't use modulus with floats, I'm lost...

Heres what I got while using int, this program converts to binary.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <iomanip>
using namespace std;


int main()
{
  int num;
		int total = 0;
		cout << "Please enter a decimal: ";
		cin >> num;
		while(num > 0)
		{
		    num /= 2;
			total = num % 2;

			cout << total << " ";
		}
    return 0;
}
any help please?
http://www.cplusplus.com/reference/clibrary/cmath/modf/

if you want convert float like 0.34 to hex
0.34 * 16 = 5.44
convert 5 to hex(.5)
0.44 * 16 = 7.04(.7)

if you stop by here, the answer would be .57
you could stop the program until the fractional number reach zero or
stop when the precision is enough for you
Last edited on
Perhaps the OP is in a different locale where that number is an integer (4050).

Try swapping lines 14 and 15.
Topic archived. No new replies allowed.