When I convert unsigned char to double, I'm losing the numbers after the decimal.

TheKidd88's Avatar, Join Date: Jun 2007
Light Poster
For example this simple program outputs 0 and not 0.9:

#include <iostream>
#include <string>
#include <fstream>
#include <stdio.h>
#include <math.h>
using namespace std;

int main()
   unsigned char x = (unsigned char)0.9;
   double y = (double) x;
   cout << y << endl;
   return 0;
How can I fix this?

Last edited by shabbir; 4Jul2007 at 09:04.. Reason: Code block
DaWei's Avatar, Join Date: Dec 2006
Team Leader
An unsigned char is an integer. It has no fractional part.

Please, once again, read the "Before you make a query" thread and learn to use code tags. These are HTML pages, which means they eat whitespace. Use the tags to preserve your formatting and indentation. It's the polite thing to do in return for the free help.