sterlinghughes closed pull request #882: libc: Add support for float in printf
URL: https://github.com/apache/mynewt-core/pull/882
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git a/libc/baselibc/src/tinyprintf.c b/libc/baselibc/src/tinyprintf.c
index 8d38d5bda..1fc262394 100644
--- a/libc/baselibc/src/tinyprintf.c
+++ b/libc/baselibc/src/tinyprintf.c
@@ -238,6 +238,8 @@ size_t tfp_format(FILE *putp, const char *fmt, va_list va)
char ch;
char lng;
void *v;
+ double d;
+ int n;
p.bf = bf;
@@ -337,6 +339,38 @@ size_t tfp_format(FILE *putp, const char *fmt, va_list va)
written += putchw(putp, &p);
p.bf = bf;
break;
+ case 'f':
+ p.base = 10;
+ d = va_arg(va, double);
+ /* Cast to an int to get the integer part of the number */
+ n = (int)d;
+ /* Convert to ascii */
+ i2a(n, &p);
+ /* Ignore left align for integer part */
+ p.left = 0;
+ /* Subtract width for decimal part and decimal point */
+ if (p.width >= 4) {
+ p.width -= 4;
+ } else {
+ p.width = 0;
+ }
+ /* Write integer part to console */
+ written += putchw(putp, &p);
+ /* Take the decimal part and multiply by 1000 */
+ n = (d-n)*1000;
+ /* Convert to asii */
+ i2a(n, &p);
+ /* Set the leading zeros for the next integer output to 3 */
+ p.lz = 3;
+ /* Always use the same decimal width */
+ p.width = 3;
+ /* Ignore sign for decimal part*/
+ p.sign = 0;
+ /* Output a decimal point */
+ putf(putp, '.');
+ /* Output the decimal part. */
+ written += putchw(putp, &p);
+ break;
case '%':
written += putf(putp, ch);
break;
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services