There is known limitation of MCUlib's sprintf() that it can't support "05.2f" format (refer 《Andes Programming guide》 Chart "Andes MCUlib" ) while newlib support this well. If customers want this feature of sprintf() and also want to choose mculib because benefits of codesize, following is a simply way to implement it.
We give an function which can pad zero to original sprintf()'s output. The prototype is:
char *float2str(char *out_str, int len, char *format, double f);
(len must be specify by user) Follow is an example which used this function:
Code:
double d1 = sin(1 + 0.1);
double d2 = sin(1 + 0.3);
unsigned int i1 = 12345678;
unsigned int i2 = 9876345;
char buf1[300];
char temp1[20];
char temp2[20];
sprintf (buf1,"/%d %2d %s /%s\r\n", i1,i2, float2str(temp1, 8, "%05.2f", d1),float2str(temp2, 13, "%08.4f", d2));
printf("string is %s\n",buf1);
The result is:
Attachment:
test1.png [ 1.17 KiB | Viewed 13844 times ]
The source code & the example demo can be found here:
Attachment:
float2str.c [347 Bytes]
Downloaded 1576 times
Attachment:
demo-sprintf.tar [610 KiB]
Downloaded 1367 times