Thread: How to convert from double to string?

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    58

    How to convert from double to string?

    Hi everyone,
    I had encounter some problem here ~ how can I convert double to string? I tried using sprintf but it seems that I encounter some problem, I wonder is it because I used mulit-thread application. any idea how I can over come the problems?

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    What some problem?

    What's wrong with:
    Code:
    char s[32];
    double d = 0.5;
    
    sprintf(s, "%Lf", d);
    ?

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    58
    I'm not sure what the problem too. If I used the code that you gave me it works pretty fine if I used it in the temp workplace I created, but when I put it into my code it doesn't work. My code spawn a thread that called the following function, it gave me odd value, eg. "s = X&4" when I added your code into my function

    The code I added to my function

    char s[32];
    double d = 0.5;

    sprintf(s, "%Lf", d);
    printf("s = %s \n",s);

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    %Lf is "long double", not double.
    Just use "%f" for floats and doubles.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    58
    I'm still getting junk after I had changed it to %f. Is there anyway to go thru it?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Post your example and output.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    58
    I use this to test the program, it should be something like this :

    Code:
    void Display_Handler
    () 
    {
    char s[32];
    double d = 0.5;
    sprintf(s, "%f", d);
    printf("Test print");
    printf("s = %s \n",s);
    }
    
    
    void Update(void* temp)
    {
    	while(repeat == TRUE){
    		/* Update */
    		Display_Handler();
    		Sleep(1000);
    	}
    }
    
    void main(void)
    {
    		_beginthread(Update, 0, NULL);
    		while(true)
    		{
    		   //do something something
    		}
    }

    my output = "s = "

    I don't think there is any problem with my code, but I just couldn't get the string printed out. I do have a linker problem "LINK : warning LNK4098: defaultlib "LIBCMT" conflicts with use of other libs; use /NODEFAULTLIB:library" does it affect the outcome?

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I don't see anything wrong there, but are you sure you want to say "nodefaultlib" to your linker? It may be that you are getting the single threaded libraries with this, and that's certainly not right in a multithreaded environment.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You certainly need to fix your linker warnings, and your void main.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    58
    I'm using visual studio 6.0 and I have set the run time library to Multi threaded DLL, did I miss somethings? what can I do so that I won't get this error message?

  11. #11
    Registered User
    Join Date
    Nov 2006
    Location
    Coimbra, Portugal
    Posts
    64
    Excuse me, but I believe the correct conversion string for a double is "%lf", and not "%f".

    Source: http://linux.die.net/man/3/scanf

    Edit: never mind. printf's conversion strings seem to behave differently than scanf's.
    Last edited by Mr_Miguel; 10-19-2007 at 03:49 AM.
    Name: Miguel Martins
    Date of birth: 14th August 1987

    "He who hesitates is lost."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String to Double function
    By jaro in forum C Programming
    Replies: 4
    Last Post: 05-27-2006, 11:10 AM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  3. Help with multi function progam
    By WackoWolf in forum C Programming
    Replies: 22
    Last Post: 10-13-2005, 02:56 AM
  4. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM