Skip to content

Commit 4700440

Browse files
committed
fix the missing minimum width requirement
1 parent 30e08c6 commit 4700440

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

hardware/esp8266com/esp8266/cores/esp8266/core_esp8266_noniso.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,7 @@ char * dtostrf(double number, signed char width, unsigned char prec, char *s) {
165165
char* out = s;
166166
// Handle negative numbers
167167
if (number < 0.0) {
168-
*out = '-';
169-
++out;
168+
*out++ = '-';
170169
number = -number;
171170
}
172171

@@ -186,20 +185,26 @@ char * dtostrf(double number, signed char width, unsigned char prec, char *s) {
186185

187186
// Print the decimal point, but only if there are digits beyond
188187
if (prec > 0) {
189-
*out = '.';
190-
++out;
188+
*out++ = '.';
191189
}
190+
// make sure the string is terminated before mesuring it length
191+
*out = 0;
192+
// Reduce minimum width accordingly
193+
width -= strlen(s);
192194

193195
// Print the digits after the decimal point
194196
int8_t digit = 0;
195197
while (prec-- > 0) {
196198
remainder *= 10.0;
197199
digit = (int8_t)remainder;
198200
if (digit > 9) digit = 9; // insurance
199-
*out = (char)('0' | digit);
200-
++out;
201+
*out++ = (char)('0' | digit);
202+
width--;
201203
remainder -= digit;
202204
}
205+
// add '0' to fill minimum width requirement
206+
while (width-- > 0) *out++ = '0';
207+
// make sure the string is terminated
203208
*out = 0;
204209
return s;
205210
}

0 commit comments

Comments
 (0)