#include <stdio.h>
void Printhex (int integer)
{
unsigned char isnegative = 0,len = 0, intstr[10]={' "};
if (Integer < 0)
{
isnegative = 1;
integer = 0-integer;
}
if (integer = = 0)
intstr[len++] = ' 0 ';
while (integer)
{
intstr[len++] = (integer% 10) + ' 0 ';
integer = INTEGER/10;
}
if (isnegative)
intstr[len++] = '-';
Note that this K data type uses K >= 0; For the end condition, K is the signed type, and if the unsigned type, the K value is rolled back to the maximum value
for (len = len; len > 0; len--)
{
printf ("%c", intstr[len-1]);
Uartsend (uart1,intstr[len-1],1);
}
}
void Printflt (float flt, int len)
void Printflt (float flt)
{
int integer;
integer = (int) Flt;
Printhex (integer);
printf (".");
Uartsend (UART1, ".", 1);
Flt = Flt-integer;
while (Flt && len--)
while (FLT)
{
FLT = Flt * 10;
integer = (int) Flt + ' 0 ';
Flt = Flt-integer;
printf ("%c", Integer);
Uartsend (uart1,&integer,1);
}
printf ("\ n");
}
int main (void)
{
float fnum = 1.12;
Printflt (Fnum, 3);
return 0;
}
--------------------------------------------------------------------------------------------------------------- ---------
#include <stdio.h>
typedef unsigned char uint8;
typedef unsigned int uint32;
Uint8 Float2char (float value, uint8 *array);
void Main ()
{
Uint8 a[20];
float value=-123.456789;
float value=-123.00152;
Uint8 i;
Float2char (value, A);
printf ("%s", A);
printf ("\ n");
}
Uint8 Float2char (float value, uint8 *array)
{
float Decimalpart;
UInt32 Integerpart;
Uint8 i = 0, j = 0, temp;
separating integers and decimals
if (value< 0)
{
value =-value;
array[i++] = '-';
}
Else
array[i++] = ' + ';
Integerpart = (uint32) value;
Decimalpart = Value-integerpart;
Working with integers
if (0 = = Integerpart)//Integer part 0
array[i++] = ' 0 ';
Else
{
while (0! = Integerpart)
{
array[i++] = integerpart% 10 + ' 0 ';
Integerpart/= 10;
}
i--;
for (j = 1; J < (i+1)/2; j + +)
{
temp = Array[j];
ARRAY[J] = array[i-j+1];
ARRAY[I-J+1] = temp;
}
i++;
}
array[i++] = '. ';
Working with decimals
array[i++] = (UInt32) (Decimalpart * 10)% 10 + ' 0 ';
array[i++] = (UInt32) (Decimalpart * 100)%10 + ' 0 ';
Array[i++]= (UInt32) (Decimalpart * 1000)%10 + ' 0 ';
array[i++] = (UInt32) (Decimalpart * 10000)%10 + ' 0 ';
Array[i] = ' + ';
return i;
}
Implementing printf floating-point printing-pending resolution