Help.. regarding string to float and float to string converion in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pardhu0163
    New Member
    • May 2012
    • 9

    Help.. regarding string to float and float to string converion in C

    Hi guys,
    I just wanted to convert the input string to float and my result get back to string format.my result is in float, long double formats).

    In my stdlib.h file, atof fuynction is there, but it's occupying huge memory when i call that function and for float to string there no function at all in that lib file.

    So, can any one help to implement those two converion functions..?

    Thanks in advance..
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    What do you mean by "occupying huge memory"? The first requirement is that the program works. Memory usage analysis is only applied after the code works so I don't know why you are worrying about it.

    To convert flot to string in C use sprintf.

    Comment

    • pardhu0163
      New Member
      • May 2012
      • 9

      #3
      Hi sir, Thank you for your reply. I used sprintf function. It's working but not accurately.plea se check out my core piece of code, and if anything wrong in that, rectify me.
      Code:
      int main( void )
      {
        char str[20];
      
        long double v=1230.0000123456f;
      	
        TFT_Init();// Initialising the TFT
      
        sprintf(str, "%f", v ); 
      
        sprint(100,50,(const unsigned char*)str,GUI_WHITE,1,2);
      // sprint is a function for printing a string on TFT.
      }
      //output:
      //1230.000000
      The out put what i am getting is not at all acceptable for my project. So, how to over come this?


      "What do you mean by "occupying huge memory"? The first requirement is that the program works. Memory usage analysis is only applied after the code works so I don't know why you are worrying about it."

      My worry is atof function is taking the little bit huge memory when i call that function. By the way i called it only once.That's why, i am thinking of replacement for atof function. I hope now you got my point.

      Thank you sir.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        This code:
        Code:
        long double v=1230.0000123456f;
        cretes a float value 1230.000. That f on the end tells the compiler to create the constant as a float. That float is then used as the initial value of the long double.

        Just omit the f:
        Code:
        long double v=1230.0000123456;   /*creates 1230.0000123456 */
        By definition all numeric constants with a decimal part are created as double.

        Comment

        • pardhu0163
          New Member
          • May 2012
          • 9

          #5
          Thank you sir,It's working fine.
          Code:
          int main( void )
          {
           long double v=123456.001;
           char str[20];
           TFT_Init();				
           sprintf(str,"%Lf",v);
           sprint(100,30,str,GUI_WHITE,1,1);
           sprintf(str,"%.3f",v);
           sprint(100,50,str,GUI_WHITE,1,1);	
          }
          //output1 : 123456.001000
          //output2 : 123456.001
          Though i am getting correct result, have to remove last three zeroes in the output1(123456. 001000). Here, it is taking by default 6 digits after point. But,in my case where i am passing the value to sprintf function, i don't know the exact precession.i mean, there may be fractional part(like 1.0002, 34.63876757) or may not be(1,123456).So , if i use this line of code,
          Code:
          sprintf(str,"%Lf",v);
          i will get irrespective of actual resultant fractional part it prints a value with 6 fractional digits. That's i want to control it.

          If any suggestions to do that, please let me know sir.

          Thank you

          regards
          pardhu

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            By default the float field width is set to 6. To widen he field, to say 12, use a width specifier: %Lf12. That should allow you to see additional digits.

            Comment

            • pardhu0163
              New Member
              • May 2012
              • 9

              #7
              yeah...that's working fine sir..

              yesterday i faced rounding off problem. I need to store atleast 20 digits answer which may be in integer value or floatng value..like our pc calculator. How can i achieve this?

              Thanks in advance.

              Regards
              Pardhu

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                Start a new thread. This is a different question from the one used to create this thread.

                Comment

                Working...