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
9 196201
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 - int main()
-
{
-
char* str = new char[30];
-
-
float flt = 2.4567F;
-
sprintf(str, "%.4g", flt );
-
cout<<str<<endl;
-
return 0;
-
}
Thankx
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. -
#include <stdio.h>
-
-
#define SIZE 10
-
-
int main(void)
-
{
-
char buf[SIZE];
-
int result = 0;
-
float value = 154.78;
-
-
result = snprintf(buf, SIZE, "%f", value);
-
if (result >= SIZE)
-
printf("The string has been truncated\n");
-
-
printf("The string value of the floating value = %s\n", buf);
-
-
return 0;
-
}
-
Here is your solution - int main()
-
{
-
char* str = new char[30];
-
-
float flt = 2.4567F;
-
sprintf(str, "%.4g", flt );
-
cout<<str<<endl;
-
return 0;
-
}
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.
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....
I think that it is defined as "_snprintf" on windows.
I think that it is defined as "_snprintf" on windows.
thankx it works fine.....
Here is your solution - int main()
-
{
-
char* str = new char[30];
-
-
float flt = 2.4567F;
-
sprintf(str, "%.4g", flt );
-
cout<<str<<endl;
-
return 0;
-
}
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
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
Same basic idea, except using an ostream: - ostringstream s;
-
s << 2.5;
-
cout << s.str();
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
by: STom |
last post by:
How do you convert a string in C# to decimal?
STom
|
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
|
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...
|
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.
|
by: FAQ server |
last post by:
-----------------------------------------------------------------------
FAQ Topic - Why does 1+1 equal 11? or How do I convert a string to a number?...
|
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!
|
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
|
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
|
by: audrey29 |
last post by:
How do i convert my string to float?
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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,...
| |