locked
Convert double to char RRS feed

  • Question

  • Hi,

    Can someone please explain to me how I convert a double variable to char, and then even how I convert it back to double from char?

    Thanks!

    Thursday, February 9, 2006 9:40 AM

Answers

  • Can't be done. You can't possibly represent a double in just one char. Or did you mean how to convert double to a character string (char *), which can be done?

    Are you allowed to use the .NET Framework for this?

     

    Thursday, February 9, 2006 7:15 PM
  • Ok The last post was correct, you can only convert a double to a char array ie, char DoubleNum[10];

    When converting a double to a char string you have to decide on the amount of decimal places. Here is the tricky part, converting a double to a char str does not automatically insert the decimal point, that is held in an integer pointer and it is up to you to insert the decimal point at the correct position. The easiest way is of course to change the char array to a CString and use the Insert() function.

    Lets begin:

    #include <stdlib.h>

    double DNum = 0; //Double Value                                                             

    char* NumChar = new char[10]; // Char String To Hold Converted Number

    int Decimal = 0; // Place Holder For Decimal Point Position

    int Sign = 0; // Used to deturmine if Double Value Is Positive Or Negative

    DNum = 100.33459; // Stupid data could be anything just for example 

    _fcvt_s(NumChar, 10, DNum, 2, &Decimal, &Sign);

    /* _s is for security only, NumChar we know is the char str, 10 is the size in bytes to be converted, DNum is the double value, 2 VERY NB the number of decimal places to convert, then decimal point and sign */

    The position in the string of the decimal point is held as an int number in Decimal. You must now modify your char string to insert the "," or ".".

    Here is an easy way:

    CString DoubleString;

    DoubleString = NumChar;

    DoubleString.Insert(Decimal, _T("."));

    Don't forget to check the sign value to see if your number is + or -.

    Convert From Char Str To Double

    This one is easier!

    #include <stdlib.h>

    double DNum = 0;

    char DChar [10]; // Place number as Char Str Here

    DNum = atof(DNum);

    Thats it!!

    Hope that helps

    Kindest Regards

    Quintin Immelman

    ZS6-IY

    Friday, February 10, 2006 7:43 AM
  • Yes it is possible but the only way I know how may not be the most elegant!

     

    Change your Edit control to a Text Box. Then check the CString char for char and where you find a ',' replace with a '.'

     

    Example:

    CString EditTxt;

    wchar_t DecChk = '\0';

     

    GetDlgItemText (IDC_EDIT1, EditTxt);

     

    for(int i = 0; i <= EditTxt.GetLength(); i++)

    {

          DecChk = EditTxt.GetAt(i);

          if(DecChk == ',')

              EditTxt.SetAt(i, '.');

    }

     

    The other thing you may want to do is to monitor the EDIT Controls EN_UPDATE message. This will allow you to catch any char the user may insert into the box by mistake. I normally display a MessageBox to indicate an error then update the EDIT Control removing the char. You can also monitor for double decimal points and prevent that. I can give you some example code on this if you wish as there are some tricks to prevent the program from crashing. I had that happen once. The customer asked for a function to be added to a program, I completed it and boastfully said "Check This" as I hit a character and the program promptly exited! Very embarrassing

     

    Hope this helps.

     

    Kindest Regards

     

    Quintin Immelman

     

    Sunday, February 12, 2006 9:09 PM

All replies

  • Can't be done. You can't possibly represent a double in just one char. Or did you mean how to convert double to a character string (char *), which can be done?

    Are you allowed to use the .NET Framework for this?

     

    Thursday, February 9, 2006 7:15 PM
  • Ok The last post was correct, you can only convert a double to a char array ie, char DoubleNum[10];

    When converting a double to a char string you have to decide on the amount of decimal places. Here is the tricky part, converting a double to a char str does not automatically insert the decimal point, that is held in an integer pointer and it is up to you to insert the decimal point at the correct position. The easiest way is of course to change the char array to a CString and use the Insert() function.

    Lets begin:

    #include <stdlib.h>

    double DNum = 0; //Double Value                                                             

    char* NumChar = new char[10]; // Char String To Hold Converted Number

    int Decimal = 0; // Place Holder For Decimal Point Position

    int Sign = 0; // Used to deturmine if Double Value Is Positive Or Negative

    DNum = 100.33459; // Stupid data could be anything just for example 

    _fcvt_s(NumChar, 10, DNum, 2, &Decimal, &Sign);

    /* _s is for security only, NumChar we know is the char str, 10 is the size in bytes to be converted, DNum is the double value, 2 VERY NB the number of decimal places to convert, then decimal point and sign */

    The position in the string of the decimal point is held as an int number in Decimal. You must now modify your char string to insert the "," or ".".

    Here is an easy way:

    CString DoubleString;

    DoubleString = NumChar;

    DoubleString.Insert(Decimal, _T("."));

    Don't forget to check the sign value to see if your number is + or -.

    Convert From Char Str To Double

    This one is easier!

    #include <stdlib.h>

    double DNum = 0;

    char DChar [10]; // Place number as Char Str Here

    DNum = atof(DNum);

    Thats it!!

    Hope that helps

    Kindest Regards

    Quintin Immelman

    ZS6-IY

    Friday, February 10, 2006 7:43 AM
  • Yes, I mean that I want to convert to and from a character string. For example:

    char Numbers[20];

    double dblNummbers = 211.5845;

    Thanks for the help!

    Friday, February 10, 2006 9:48 AM
  • I've got some trouble here. When I convert the double variable to character string, the result will be wrong. The digits after the decimal point is only zeros.

    If I convert this to a character string:

    double dNum = 245,84;

    the result will be: 245,00

    Why? I want it to be exactly the same number, not zeros after the decimal point.

    Sunday, February 12, 2006 3:31 PM
  • Show your code please.

     Orjan Lindberg wrote:

    I've got some trouble here. When I convert the double variable to character string, the result will be wrong. The digits after the decimal point is only zeros.

    If I convert this to a character string:

    double dNum = 245,84;

    the result will be: 245,00

    Why? I want it to be exactly the same number, not zeros after the decimal point.

    Sunday, February 12, 2006 3:42 PM
  • here is the code:

    char* cNum = new char[50];
    double dNUM = dlg.m_EditCtrl1;      //m_EditCtrl1 contains the number to be converted.
    _fcvt_s(cNum, 50, dNUM, 2, &decimal, &sign);
    CString str = cNum;
    str.Insert(decimal, _T(","));

    Sunday, February 12, 2006 3:57 PM
  • The following code :-

    int decimal;
    int sign;
    char cNum[50];
    double dNUM = 210.19;
    _fcvt_s(cNum, 50, dNUM, 2, &decimal, &sign);
    CString str = CA2T(cNum);
    str.Insert(decimal,_T('.'));
    MessageBox(str); //correctly shows 210.19

    ...works fine for me. Looks like your edit control DDX variable is associated with an int which results in truncation of the decimal point fraction.

     Orjan Lindberg wrote:

    here is the code:

    char* cNum = new char[50];
    double dNUM = dlg.m_EditCtrl1;      //m_EditCtrl1 contains the number to be converted.
    _fcvt_s(cNum, 50, dNUM, 2, &decimal, &sign);
    CString str = cNum;
    str.Insert(decimal, _T(","));

    Sunday, February 12, 2006 5:09 PM
  • 'Looks like your edit control DDX variable is associated with an int which results in truncation of the decimal point fraction.'

    No, the edit control variable is declared as a double.

    But I think I know what's causes this now. I use the ',' as decimal pointer in the edit control. But if I use the '.' as decimal point, the convertion will be correct. How should I do if I want to use ',' as decimal point in the edit control? Is it possible?

    Sunday, February 12, 2006 5:57 PM
  • Yes it is possible but the only way I know how may not be the most elegant!

     

    Change your Edit control to a Text Box. Then check the CString char for char and where you find a ',' replace with a '.'

     

    Example:

    CString EditTxt;

    wchar_t DecChk = '\0';

     

    GetDlgItemText (IDC_EDIT1, EditTxt);

     

    for(int i = 0; i <= EditTxt.GetLength(); i++)

    {

          DecChk = EditTxt.GetAt(i);

          if(DecChk == ',')

              EditTxt.SetAt(i, '.');

    }

     

    The other thing you may want to do is to monitor the EDIT Controls EN_UPDATE message. This will allow you to catch any char the user may insert into the box by mistake. I normally display a MessageBox to indicate an error then update the EDIT Control removing the char. You can also monitor for double decimal points and prevent that. I can give you some example code on this if you wish as there are some tricks to prevent the program from crashing. I had that happen once. The customer asked for a function to be added to a program, I completed it and boastfully said "Check This" as I hit a character and the program promptly exited! Very embarrassing

     

    Hope this helps.

     

    Kindest Regards

     

    Quintin Immelman

     

    Sunday, February 12, 2006 9:09 PM
  • Ok, thanks very much for the help!
    Monday, February 13, 2006 9:01 AM
  • Look at the sprintf() function for converting a double to a string, it is easy and does not require inserting the decimal point.




    Monday, February 13, 2006 6:05 PM
  • 'Look at the sprintf() function for converting a double to a string, it is easy and does not require inserting the decimal point.'

    hmm...that was something new to me. Maybe I can use that instead of _fcvt_s. Thanks!

    Monday, February 13, 2006 10:07 PM