Ftoa float string to ftoa float string
# Include <stdio. h>
Bool ftos (float num, char * s, int n)
{
Int temp;
Float t = num;
Int pn = 0;
Bool flag_s = true;
Bool flag_z = false;
For (int I = n; I> 0; I --)
T = t * 10;
Printf ("% f \ n", t );
Temp = t;
Printf ("% d \ n", temp );
If (temp <0)
{
Temp =-temp;
T =-num;
Flag_s = false;
}
If (temp = 0)
{
If (n> 0)
{
For (pn = 0; pn <n; pn ++)
{
* (S + pn) = '0 ';
}
* (S + pn) = '\.';
++ Pn;
}
* (S + pn) = '0 ';
++ Pn;
If (! Flag_s ){
* (S + pn) = '\-';
++ Pn;
}
* (S + pn) = '\ 0 ';
}
Else
{
While (temp> 0)
{
If (pn = n & n> 0)
{
* (S + pn) = '\.';
++ Pn;
}
Else {
* (S + pn) = (temp % 10) + '0 ';
Temp = temp/10;
++ Pn;
}
}
While (pn <n)
{
* (S + pn) = '0 ';
++ Pn;
}
If (pn = n)
{
* (S + pn) = '\.';
++ Pn;
If (flag_s ){
* (S + pn) = '0 ';
++ Pn;
}
}
If (! Flag_s ){
If (t <1.0 ){
* (S + pn) = '\.';
++ Pn;
* (S + pn) = '0 ';
++ Pn;
}
* (S + pn) = '\-';
++ Pn;
}
* (S + pn) = '\ 0 ';
}
Int I (0), j (pn-1 );
While (I <j)
{
Int temp = * (s + I );
* (S + I) = * (s + j );
* (S + j) = temp;
++ I;
J --;
}
Return true;
}
Int main ()
{
Char s [20];
Float num = 123.456;
Int n = 3;
Scanf ("% f", & num );
Scanf ("% d", & n );
Printf ("float num is % f \ n", num );
If (ftos (num, s, n ))
{
Printf ("after convert is % s \ n", s );
}
Return 0;
}
What is the function used to convert the floating point type to the string in VC ++?
The usage of sprintf is similar to that of printf. The difference is that printf outputs data to the screen, while sprintf outputs data to strings.
Char buffer [100];
Sprintf ("sprintf hello world % d % f \ n", 1, 1.0f );
Printf ("% s", buffer );
Single-Chip Microcomputer: I want to convert a float variable to a string using the following methods: Time = 5 * count; SecondLine = '0' + Time; Error
This method only applies to char characters.
If you use a library, you can use functions such as ftoa itoa
If no database is used, you need to convert Integer type to string type and floating point to string type.
Train of Thought: Convert INTEGER: n % 10 + '0' to one-digit Conversion
Floating Point: If you need two decimal places, f * 100 is converted to an integer type, and then the above idea is used as an example.