Thread: float to string reverse atof();

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    247

    float to string reverse atof();

    Hi guys,
    its not often I ask questions on here, but I am on the lookout for a function that will perform the reverse of atof( ); in any library. I have tried to code my own but to no avail.

    what I am looking for is to convert say 1234567.89 to printable form 1,234,567.89, even if I could get the float to convert to string form "1234567.89", then I could manipulate the string.

    any pointers would be helpful.

    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    #include <stdlib.h>

    char *_ecvt( double value, int count, int *dec, int *sign );

    _ecvt returns a pointer to the string of digits. There is no error return.

    Parameters

    value

    Number to be converted

    count

    Number of digits stored

    dec

    Stored decimal-point position

    sign

    Sign of converted number
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  3. #3
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    oh i amost forgot, you have to place the decimal point and the sign in the string yourself...
    Last edited by no-one; 01-30-2002 at 03:08 PM.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    20

    char to float

    On a sort of related topic - I am having a problem getting the correct figure printed from a file.

    I read a record from a binary file into a structure as below:

    struct record{
    char name [21];
    char street [41];
    char balance [10];
    char limit [10];
    };

    When I look at the file data, balance reads as 000010000

    I then want to read this record into a new structure as below:

    struct new{
    char cust_name [21];
    char cust_street [41];
    float cust_balance;
    long cust_limit;
    };

    I thought the following would convert the balance from char to float:

    newstruct_ptr = atof(oldstruct_ptr);

    This structure is then written to a new file.

    However, when I come to print out the new file I get 10000 instead of the correct 100.00

    For the long I used atol which seemed to work OK.

    Why won't the above work? How do I convert from char to float to get the correct print out?

  5. #5
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    i don't know really without seeing code, but heres an example use that works.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main(void)
    {
        char num[] = " 12.8768";
        double x = atof(num);
        
        printf("%s is %f\n",num,x);
    
        return 0;
    }
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    20
    Let me see if I can explain it any better.

    char balance [10];

    has read into it from a binary file 000010000, 9 characters and a null. So unlike your example there is no decimal place set in the original balance. This number is actually 100.00, therefore when I come to read it into the new structure I put:

    newstruct_ptr->cust_balance = atof(oldstruct_ptr->balance);

    expecting this to put in the 2 decimal places.

    In the new structure it is

    float cust_balance;

    But this is not working I am getting the figure printed out as 10000.

    Help!!

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    Hi Cyber,
    program 3,
    to transfer new customer data to master file structure, create a seperate structure identifier for the type master structure.
    Code:
    void new_cust (structure pointer for transaction file)
    {
       struct master new_customer;
       float converted_bal;
       long converted_creditlimit;
    
       strcpy(new_customer.cust_code, transaction.cust_code);
       same for address and name.
       converted_bal = atof(transaction.cust_bal);
       converted_creditlimit = atol(tranaction.credit_limit);
       new_customer.balance = converted_bal;
       new_customer.credit_limit = converted_creditlimit;
       use time structure for yymmdd;
       write to customer master file;
    }
    Hi was typing this when you posted.
    try
    converted_bal = atof(transaction.cust_bal)/100;
    Last edited by bigtamscot; 01-30-2002 at 04:26 PM.
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  8. #8
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    let me rectify my earlier entry
    to convert 000010000 to 100.00
    do this

    converted_balance = (float)(atoi(allrecs->iss_ret.balance)/100;
    new_customer.balance = converted_balance;

    OR

    newstruct_ptr->cust_balance = atof(oldstruct_ptr->balance)/100;
    Last edited by bigtamscot; 01-30-2002 at 05:00 PM.
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-13-2009, 03:25 PM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM