364,036 Members | 5023 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

How do i convert a float to string?

cool17
P: 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
Share this Question
Share on Google+
9 Replies


vermarajeev
100+
P: 180
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
100+
P: 144
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
100+
P: 144
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
100+
P: 180
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
100+
P: 144
I think that it is defined as "_snprintf" on windows.
Oct 18 '06 #6

vermarajeev
100+
P: 180
I think that it is defined as "_snprintf" on windows.
thankx it works fine.....
Oct 18 '06 #7

srvardhan
P: 1
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
Expert 5K+
P: 8,599
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

NinjaSkitch
P: 1
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

Help answer this question



Didn't find the answer to your C / C++ question?

You can also browse similar questions: C / C++ float в строку float tostring