473,394 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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..
May 27 '12 #1
7 2091
weaknessforcats
9,208 Expert Mod 8TB
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.
May 27 '12 #2
Hi sir, Thank you for your reply. I used sprintf function. It's working but not accurately.please check out my core piece of code, and if anything wrong in that, rectify me.
Expand|Select|Wrap|Line Numbers
  1. int main( void )
  2. {
  3.   char str[20];
  4.  
  5.   long double v=1230.0000123456f;
  6.  
  7.   TFT_Init();// Initialising the TFT
  8.  
  9.   sprintf(str, "%f", v ); 
  10.  
  11.   sprint(100,50,(const unsigned char*)str,GUI_WHITE,1,2);
  12. // sprint is a function for printing a string on TFT.
  13. }
  14. //output:
  15. //1230.000000
  16.  
  17.  
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.
May 28 '12 #3
weaknessforcats
9,208 Expert Mod 8TB
This code:
Expand|Select|Wrap|Line Numbers
  1. 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:
Expand|Select|Wrap|Line Numbers
  1. long double v=1230.0000123456;   /*creates 1230.0000123456 */
By definition all numeric constants with a decimal part are created as double.
May 28 '12 #4
Thank you sir,It's working fine.
Expand|Select|Wrap|Line Numbers
  1. int main( void )
  2. {
  3.  long double v=123456.001;
  4.  char str[20];
  5.  TFT_Init();                
  6.  sprintf(str,"%Lf",v);
  7.  sprint(100,30,str,GUI_WHITE,1,1);
  8.  sprintf(str,"%.3f",v);
  9.  sprint(100,50,str,GUI_WHITE,1,1);    
  10. }
  11. //output1 : 123456.001000
  12. //output2 : 123456.001
  13.  
  14.  
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,
Expand|Select|Wrap|Line Numbers
  1. 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
May 28 '12 #5
weaknessforcats
9,208 Expert Mod 8TB
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.
May 28 '12 #6
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
May 29 '12 #7
weaknessforcats
9,208 Expert Mod 8TB
Start a new thread. This is a different question from the one used to create this thread.
May 29 '12 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Alan | last post by:
hi, are there any functions that can change double and float to string ? and is it possible to change the double/float to string which only contains decimal format instead of exponent...
5
by: Gustaf Liljegren | last post by:
Values altered when I convert from string to float and back again: using System; class FloatTest { static void Main(string args) { string var1 = "800856.22"; Console.WriteLine(var1);
6
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards,...
9
by: Python.LeoJay | last post by:
Dear all, i need to parse billions of numbers from a file into float numbers for further calculation. i'm not satisfied with the speed of atof() function on my machine(i'm using visual c++ 6)....
3
by: SharpCoderMP | last post by:
i've run into some trouble using data from xml inside my app. the scenario is simple. input data looks more or less like this: <item> <name>MyName</name> <somefloat>11.5</somefloat> </item> ...
6
by: trevor | last post by:
Incorrect values when using float.Parse(string) I have discovered a problem with float.Parse(string) not getting values exactly correct in some circumstances(CSV file source) but in very similar...
3
by: pipe.jack | last post by:
I'm trying to convert string to float and my float after conversion is 0 (zero). Here is my code: $c = $currencies->format($cart->show_total()); echo gettype($c); echo (float)$c; $c = 39.59...
2
by: lisasahu | last post by:
how to convert float to string like there is atof atoi and itoa but is ther any method to convert float to string
0
by: Tyler Breisacher | last post by:
It's generally a bad idea to use "except" without naming a specific exception. The exception you might expect in this case is ValueError. Any other exception *should* be uncaught if it happens. By...
0
by: Timothy Grant | last post by:
That's because s IS a string. It's not been converted to a float. In : s = '3.1415' In : n = float(s) In : type(s) Out: <type 'str'> In : type(n) Out: <type 'float'> Why are you avoiding...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.