468,490 Members | 2,553 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,490 developers. It's quick & easy.

How do i convert a float to string?

24
hi guys

I am writing a code for to covert expression to string so how is this going to be possible in C?

i know for string to float is using atof() so what about the other way round?

thx
Oct 18 '06 #1
9 194468
vermarajeev
180 100+
hi guys

I am writing a code for to covert expression to string so how is this going to be possible in C?

i know for string to float is using atof() so what about the other way round?

thx
Here is your solution

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     char* str = new char[30];
  4.  
  5.     float flt = 2.4567F;
  6.     sprintf(str, "%.4g", flt );    
  7.     cout<<str<<endl;
  8.     return 0;
  9. }
Thankx
Oct 18 '06 #2
tyreld
144 100+
You can use "snprintf" to accomplish this.
int snprintf(char *str, size_t size, const char *format, ...);
Upon successful return, this function returns the number of characters printed (not including the trailing '\0' used to end output to strings). The function snprintf() does not write more than size characters (including the trailing '\0'). If the output was truncated due to this limit then the return value is the number of characters (not including the trailing '\0') which would have been written to the final string if enough space had been available. Thus, a return value of size or more means that the output was truncated. (See also below under NOTES.) If an output error is encountered, a negative value is returned.

You will notice in this example that if you run it the string is reported as truncated. This is because by default the %f format has a percision of 6 decimal places. Change %f to %.2f and rerun it.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. #define SIZE 10
  4.  
  5. int main(void)
  6. {
  7.    char buf[SIZE];
  8.    int result = 0;
  9.    float value = 154.78;
  10.  
  11.    result = snprintf(buf, SIZE, "%f", value);
  12.    if (result >= SIZE)
  13.        printf("The string has been truncated\n");
  14.  
  15.    printf("The string value of the floating value = %s\n", buf);
  16.  
  17.    return 0;
  18. }
  19.  
Oct 18 '06 #3
tyreld
144 100+
Here is your solution

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     char* str = new char[30];
  4.  
  5.     float flt = 2.4567F;
  6.     sprintf(str, "%.4g", flt );    
  7.     cout<<str<<endl;
  8.     return 0;
  9. }
Thankx
Using "sprintf" is a poor choice as it can overrun the provided string buffer if you are careless. Using functions that allow you to control the the size of input copied into a buffer is always the best coding practice. In this case "snprintf" is a better choice.
Oct 18 '06 #4
vermarajeev
180 100+
I get this error when I use snprintf

error C3861: 'snprintf': identifier not found, even with argument-dependent lookup

I'm using Microsoft Visual Studio .NET on my system

Thankx....
Oct 18 '06 #5
tyreld
144 100+
I think that it is defined as "_snprintf" on windows.
Oct 18 '06 #6
vermarajeev
180 100+
I think that it is defined as "_snprintf" on windows.
thankx it works fine.....
Oct 18 '06 #7
Here is your solution

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     char* str = new char[30];
  4.  
  5.     float flt = 2.4567F;
  6.     sprintf(str, "%.4g", flt );    
  7.     cout<<str<<endl;
  8.     return 0;
  9. }
Thankx

There is one easy method using this u can convert from any data type to any other data type

Just include the sstream header file

let
string strSalary = "5449.98";
float SalaryInFloat;

istringstream mySalaray(strSalary);
mySalaray >> SalaryInFloat

thats it

Rajavardhan Sarkapally
Oct 14 '07 #8
JosAH
11,448 Expert 8TB
There is one easy method using this u can convert from string data type to any other data type

Just include the sstream header file

let
string strSalary = "5449.98";
float SalaryInFloat;

istringstream mySalaray(strSalary);
mySalaray >> SalaryInFloat

thats it

Rajavardhan Sarkapally
Ern, the OP asked for something the other way around if I'm not mistaken.

kind regards,

Jos
Oct 14 '07 #9
Same basic idea, except using an ostream:

Expand|Select|Wrap|Line Numbers
  1. ostringstream s;
  2. s << 2.5;
  3. cout << s.str();
Nov 27 '07 #10

Post your reply

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

Similar topics

3 posts views Thread by Convert TextBox.Text to Int32 Problem | last post: by
4 posts views Thread by STom | last post: by
15 posts views Thread by Yifan | last post: by
3 posts views Thread by priyanka | last post: by
2 posts views Thread by lisasahu | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by theflame83 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.