Skip to content

Commit b600b28

Browse files
committed
Fix dtostrf() with value in range -1.0 < val < 0.0
Fix #513 Signed-off-by: Frederic.Pillon <[email protected]>
1 parent 03557f3 commit b600b28

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

cores/arduino/avr/dtostrf.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ char *dtostrf(double val, signed char width, unsigned char prec, char *sout)
3131
return sout;*/
3232

3333
// Handle negative numbers
34-
unsigned int negative = 0;
34+
uint8_t negative = 0;
3535
if (val < 0.0) {
3636
negative = 1;
3737
val = -val;
@@ -49,10 +49,6 @@ char *dtostrf(double val, signed char width, unsigned char prec, char *sout)
4949
unsigned long int_part = (unsigned long)val;
5050
double remainder = val - (double)int_part;
5151

52-
if (negative) {
53-
int_part = -int_part;
54-
}
55-
5652
// Extract digits from the remainder
5753
unsigned long dec_part = 0;
5854
double decade = 1.0;
@@ -62,8 +58,11 @@ char *dtostrf(double val, signed char width, unsigned char prec, char *sout)
6258
remainder *= decade;
6359
dec_part = (int)remainder;
6460

65-
sprintf(sout, "%ld.%0*ld", int_part, prec, dec_part);
66-
61+
if (negative) {
62+
sprintf(sout, "-%ld.%0*ld", int_part, prec, dec_part);
63+
} else {
64+
sprintf(sout, "%ld.%0*ld", int_part, prec, dec_part);
65+
}
6766
// Handle minimum field width of the output string
6867
// width is signed value, negative for left adjustment.
6968
// Range -128,127

0 commit comments

Comments
 (0)