Double to String

This is a discussion on Double to String within the C Programming forums, part of the General Programming Boards category; hi, how to convert the DOUBLE date into a string without using some certain function of the library? I need ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    4

    Double to String

    hi,
    how to convert the DOUBLE date into a string without using some certain function of the library?
    I need your code(name.c)
    thx in advance.
    happy new year!

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Post a bit of your code that you've tried so far.

    Also, you might want to read this:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    4
    but "sprintf""snprintf""fprintf"are all functions of the library.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    So you want to do this yourself? Do it on paper first, then translate that to psuedo code, then real code. Have fun

    Just in case you don't know, this is an educational forum, where we help others learn. It's not a place to get free coding done. Make some effort, post the code you've tried and someone will help you.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    4
    I am confesed by my own code:
    When I entered the double date:4545.6767.the output is 4545.676699999999999.Why?what should I do ?
    Code:
    #include <stdio.h>
    #include <string.h>
    #define Array_MaxValue 17 
    
    
    char *MY_DoubleToChar(double db,char *db_str)
    {
        int DecP=1,loop;
        double _db;
        int db_arr[Array_MaxValue];
        
        if(db>=0.0)
        {
            while(db>=10.0)
            {
                DecP++;
                db/=10;
            }
            for(loop=0;loop<17;loop++)
            {
                if(loop==DecP)
                {
                    db_arr[loop]=-2;
                    continue;
                }
                db_arr[loop]=(int)db;
                db-=db_arr[loop]; 
                db*=10.0;
            }
            for(loop=0;loop<17;loop++)
                db_str[loop]=db_arr[loop]+48; 
            return(db_str);
        }
        else
        {
            _db=-db;
            while(_db>=10.0)
            {
                DecP++;
                _db/=10;
            }
            for(loop=0;loop<17;loop++)
            {
                if(loop==DecP)
                {
                    db_arr[loop]=-2;
                    continue;
                }
                db_arr[loop]=(int)(_db);
                _db-=db_arr[loop];
                _db*=10.0;
            }
            for(loop=0;loop<17;loop++)
                db_str[loop]=db_arr[loop]+48;  
            return(db_str);
        } 
    }
    
    int main()
    {
        const double i;
        char *str;
        char *sign;
        str=(char *)malloc(sizeof(char)*Array_MaxValue);
        sign=(char *)malloc(sizeof(char));
        printf("enter the DOUBLE date:\n");
        scanf("%lf",&i);
        if(i<0.0)
        {
            *sign='-';
            printf("%c",*sign);
            printf("%s",MY_DoubleToChar(i,str));
        }
        else
            printf("%s",MY_DoubleToChar(i,str));
        free(str);
        getch();
        return 0;
    }

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >sign=(char *)malloc(sizeof(char));
    Why are you using malloc for a single char?

    >const double i;
    You don't treat this as const, so don't declare it as const.

    >str=(char *)malloc(sizeof(char)*Array_MaxValue);
    You forgot to include stdlib.h and hid that error by casting the result of malloc. See the FAQ concerning this issue and you'll learn how to call malloc properly.

    >if(db>=0.0)
    Beware floating-point comparisons, they can be tricky.

    >the output is 4545.676699999999999
    Try walking through your code. Does each instruction do as you expect? If not, try and figure out why it does what it does.
    My best code is written with the delete key.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > the output is 4545.676699999999999.Why?
    Perhaps you should also be reading the background material I posted in this thread
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > the output is 4545.676699999999999.Why?
    Perhaps you should also be reading the background material I posted in this thread
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 03:44 PM
  2. C++ to C Conversion
    By dicon in forum C Programming
    Replies: 7
    Last Post: 06-11-2007, 08:38 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  4. Help with multi function progam
    By WackoWolf in forum C Programming
    Replies: 22
    Last Post: 10-13-2005, 02:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21