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

Discussion in 'C++' started by TheKidd88, Jul 3, 2007.

  1. TheKidd88

    TheKidd88 New Member

    Joined:
    Jun 28, 2007
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    For example this simple program outputs 0 and not 0.9:

    Code:
    #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 a moderator: Jul 4, 2007
  2. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    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.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice