Skip to content

Commit d0e37b0

Browse files
James Fosterjgfoster
James Foster
andauthored
Replacement for prior commit. This one is a branch from master instead of main. (#313)
Co-authored-by: James Foster <[email protected]>
1 parent f10e497 commit d0e37b0

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1111
- Show output from successful compile
1212
- `--min-free-space=N` command-line argument to fail if free space is below requred value
1313
- Add `_BV()` macro.
14+
- Support for `dtostrf()`
1415

1516
### Changed
1617
- Fix copy/paste error to allow additional warnings for a platform

SampleProjects/TestSomething/test/stdlib.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,12 @@ unittest(library_tests_itoa)
4343

4444
}
4545

46+
unittest(library_tests_dtostrf)
47+
{
48+
float num = 123.456;
49+
char buffer[10];
50+
dtostrf(num, 7, 3, buffer);
51+
assertEqual(strncmp(buffer, "123.456", sizeof(buffer)), 0);
52+
}
53+
4654
unittest_main()

cpp/arduino/stdlib.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
** is out of range.
1919
*/
2020

21+
#include <stdio.h>
2122
#include <stdlib.h>
2223
#include <string.h>
2324

@@ -59,3 +60,22 @@ char *itoa(int N, char *str, int base)
5960
return str;
6061
}
6162
#endif
63+
64+
/*
65+
The dtostrf() function converts the double value passed in val into
66+
an ASCII representationthat will be stored under s. The caller is
67+
responsible for providing sufficient storage in s.
68+
69+
Conversion is done in the format “[-]d.ddd”. The minimum field width
70+
of the output string (including the ‘.’ and the possible sign for
71+
negative values) is given in width, and prec determines the number of
72+
digits after the decimal sign. width is signed value, negative for
73+
left adjustment.
74+
75+
The dtostrf() function returns the pointer to the converted string s.
76+
*/
77+
78+
char *dtostrf(double __val, signed char __width, unsigned char __prec, char *__s) {
79+
sprintf(__s, "%*.*f", __width, __prec, __val);
80+
return __s;
81+
}

cpp/arduino/stdlib.h

+3
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@
1212
* https://stackoverflow.com/questions/190229/where-is-the-itoa-function-in-linux
1313
*/
1414
char *itoa(int val, char *s, int radix);
15+
16+
// another function provided by Arduino
17+
char * dtostrf(double __val, signed char __width, unsigned char __prec, char *__s);

0 commit comments

Comments
 (0)