summaryrefslogtreecommitdiffstatshomepage
path: root/py
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2017-06-13 13:36:56 +1000
committerDamien George <damien.p.george@gmail.com>2017-06-13 13:36:56 +1000
commit6ed4581f545069488446682ee9b052798626327d (patch)
tree757e98d329b1afa57c7b31d68b85709141d74dd9 /py
parentd42b80fd648d74e2c6f66626e904103091ee9334 (diff)
downloadmicropython-6ed4581f545069488446682ee9b052798626327d.tar.gz
micropython-6ed4581f545069488446682ee9b052798626327d.zip
py/formatfloat: Fix number of digits and exponent sign when rounding.
This patch fixes 2 things when printing a floating-point number that requires rounding up of the mantissa: - retain the correct precision; eg 0.99 becomes 1.0, not 1.00 - if the exponent goes from -1 to 0 then render it as +0, not -0
Diffstat (limited to 'py')
-rw-r--r--py/formatfloat.c7
1 files changed, 6 insertions, 1 deletions
diff --git a/py/formatfloat.c b/py/formatfloat.c
index 9ff80d9f63..b16746b39b 100644
--- a/py/formatfloat.c
+++ b/py/formatfloat.c
@@ -376,11 +376,16 @@ int mp_format_float(FPTYPE f, char *buf, size_t buf_size, char fmt, int prec, ch
rs[1] = '0';
if (e_sign == '-') {
e--;
+ if (e == 0) {
+ e_sign = '+';
+ }
} else {
e++;
}
+ } else {
+ // Need at extra digit at the end to make room for the leading '1'
+ s++;
}
- s++;
char *ss = s;
while (ss > rs) {
*ss = ss[-1];