summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2014-01-12 16:26:20 -0800
committerDamien George <damien.p.george@gmail.com>2014-01-12 16:26:20 -0800
commited3a32b117dfa541136d1a97d966ba7292c9c057 (patch)
tree7dfafbec9e8636b449dff0d3159ed39c8a79ee8c
parent328708eb2504eae2ca8132c948bc3b1fc1c95243 (diff)
parentb5f458278ca535bb03e84220c6db0d8ecd979586 (diff)
downloadmicropython-ed3a32b117dfa541136d1a97d966ba7292c9c057.tar.gz
micropython-ed3a32b117dfa541136d1a97d966ba7292c9c057.zip
Merge pull request #157 from dhylands/printf-float
Added a hacky implementation for %g
-rw-r--r--stm/printf.c15
1 files changed, 15 insertions, 0 deletions
diff --git a/stm/printf.c b/stm/printf.c
index c644cd94ff..c0fa82e1b0 100644
--- a/stm/printf.c
+++ b/stm/printf.c
@@ -206,6 +206,21 @@ int pfenv_printf(const pfenv_t *pfenv, const char *fmt, va_list args) {
case 'P': // ?
chrs += pfenv_print_int(pfenv, va_arg(args, int), 0, 16, 'A', flags, width);
break;
+ case 'g':
+ {
+ // This is a very hacky approach to printing floats. Micropython
+ // uses %g when using print, and I just wanted to see somthing
+ // usable. I expect that this will be replaced with something
+ // more appropriate.
+ char dot = '.';
+ double d = va_arg(args, double);
+ int left = (int)d;
+ int right = (int)((d - (double)(int)d) * 1000000.0);
+ chrs += pfenv_print_int(pfenv, left, 1, 10, 'a', flags, width);
+ chrs += pfenv_print_strn(pfenv, &dot, 1, flags, width);
+ chrs += pfenv_print_int(pfenv, right, 0, 10, 'a', PF_FLAG_ZERO_PAD, 6);
+ break;
+ }
default:
pfenv->print_strn(pfenv->data, fmt, 1);
chrs += 1;