Find the answer to your Linux question:
Results 1 to 6 of 6
I need to convert in C a string to a float with 1 decimal: %s -> %.1f Code: char l1[1024]; unsigned long long *convert; l1 -> 1024.1 (gets this string ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Feb 2005
    Posts
    9

    C code to convert a string to a float with 1 decimal


    I need to convert in C a string to a float with 1 decimal:

    %s -> %.1f
    Code:
    char l1[1024];
    unsigned long long *convert;
    l1 -> 1024.1 (gets this string from file)
    
    
    *convert = strtoull(l1, &l1, 0); (I'm not so sure here)
    printf("%.1f\n", convert); (and I need to printf it but there's something missing)
    What am I doing wrong ?

    Any help is appreciated.

  2. #2
    Just Joined!
    Join Date
    Feb 2005
    Location
    London UK
    Posts
    9

    string to float

    First off you are using a function which converts to a long not a float. atof is more likely to do what you want.

  3. #3
    Just Joined!
    Join Date
    Feb 2005
    Posts
    9
    Thanks.

  4. #4
    Linux Newbie
    Join Date
    Oct 2004
    Posts
    158
    Try:
    Code:
    #include <stdlib.h>
    int main&#40;&#41;
    &#123;
       float a=atof&#40;"130.02"&#41;;	
       float b=atof&#40;"12345.12"&#41;;	
       float c=atof&#40;"12345.67"&#41;;	        
       printf&#40;"%.1f %.1f %.1f\n", a,b,c&#41;;
       return 0;
    &#125;
    Floating point numbers (internally) do not exactly represent some numbers the way they are entered into the program. You have to use printf(), sprintf() or write a rounding function to actually get a correct value. There is no workaround, except to use integers:

    This is definitely the HARD WAY:
    Code:
    #include <stdlib.h>
    #include <math.h>
    
    void my_round&#40;char *src&#41;
    &#123;
       double f=atof&#40;src&#41;;
       double tmp=0; 
       double dec=0;
       double whole=0;
       int whole_number=0,
           decimal=0;
       dec=modf&#40;f,&whole&#41;;
       whole_number=whole;
       dec=modf&#40;dec*10,&tmp&#41;;
       decimal=tmp;
       printf&#40;"%d.%d\n", whole_number,decimal&#41;;   
    &#125;
    int main&#40;&#41;
    &#123;
       my_round&#40;"130.02"&#41;;	
       my_round&#40;"12345.12"&#41;;	
       my_round&#40;"12345.67"&#41;;	        
    
       return 0;
    &#125;

  5. #5
    Pim
    Pim is offline
    Just Joined!
    Join Date
    Feb 2005
    Location
    Groningen (NL)
    Posts
    19

    recommendation

    Hmm,
    I guess you should build a small parser.
    I mean you can go through each character in the array, can't you?
    They're only numerical....
    A switch() would be nice =)

    amazing

  6. #6
    Just Joined!
    Join Date
    Feb 2011
    Posts
    1
    Quote Originally Posted by Jake View Post
    First off you are using a function which converts to a long not a float. atof is more likely to do what you want.
    Or in C99 terms, because atof( ) was dropped:


    double num;
    num = strtod( "3.14159", NULL ); // converts to floating point
    printf( "%8.3f", num ); // prints something like "3.142"


    -- pete at pwilson dot net

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •