473,385 Members | 1,927 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,385 software developers and data experts.

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 196201
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

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

Similar topics

3
by: Convert TextBox.Text to Int32 Problem | last post by:
Need a little help here. I saw some related posts, so here goes... I have some textboxes which are designed for the user to enter a integer value. In "old school C" we just used the atoi function...
4
by: STom | last post by:
How do you convert a string in C# to decimal? STom
15
by: Yifan | last post by:
Hi Does anybody know how to convert System::String* to char*? I searched the System::String class members and did not find any. Thanks Yifan
4
by: Pixie Songbook | last post by:
Hi, I'm trying to write a program using the Dev C++ 4.9.9.2 compiler that takes input numbers from a word document, sums them together, and then gives me a result. It should be easy as the book I...
3
by: priyanka | last post by:
Hi there, I want to convert a String into integer. I get the string from a file using : string argNum; getline(inputStream,argNum); I now need to convert argNum into integer.
1
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does 1+1 equal 11? or How do I convert a string to a number?...
7
by: shellon | last post by:
Hi all: I want to convert the float number to sortable integer, like the function float2rawInt() in java, but I don't know the internal expression of float, appreciate your help!
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
5
by: sidhuasp | last post by:
Hi could anyone please tell me how to convert float value in my database ti HOUR/MIN format for my timesheet requirement. i have to do it in sql-reporting services
3
by: audrey29 | last post by:
How do i convert my string to float?
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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,...

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.