Skip to content

Add support for dtostrf() #313

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Show output from successful compile
- `--min-free-space=N` command-line argument to fail if free space is below requred value
- Add `_BV()` macro.
- Support for `dtostrf()`

### Changed
- Fix copy/paste error to allow additional warnings for a platform
Expand Down
8 changes: 8 additions & 0 deletions SampleProjects/TestSomething/test/stdlib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,12 @@ unittest(library_tests_itoa)

}

unittest(library_tests_dtostrf)
{
float num = 123.456;
char buffer[10];
dtostrf(num, 7, 3, buffer);
assertEqual(strncmp(buffer, "123.456", sizeof(buffer)), 0);
}

unittest_main()
20 changes: 20 additions & 0 deletions cpp/arduino/stdlib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
** is out of range.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

Expand Down Expand Up @@ -59,3 +60,22 @@ char *itoa(int N, char *str, int base)
return str;
}
#endif

/*
The dtostrf() function converts the double value passed in val into
an ASCII representationthat will be stored under s. The caller is
responsible for providing sufficient storage in s.

Conversion is done in the format “[-]d.ddd”. The minimum field width
of the output string (including the ‘.’ and the possible sign for
negative values) is given in width, and prec determines the number of
digits after the decimal sign. width is signed value, negative for
left adjustment.

The dtostrf() function returns the pointer to the converted string s.
*/

char *dtostrf(double __val, signed char __width, unsigned char __prec, char *__s) {
sprintf(__s, "%*.*f", __width, __prec, __val);
return __s;
}
3 changes: 3 additions & 0 deletions cpp/arduino/stdlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@
* https://stackoverflow.com/questions/190229/where-is-the-itoa-function-in-linux
*/
char *itoa(int val, char *s, int radix);

// another function provided by Arduino
char * dtostrf(double __val, signed char __width, unsigned char __prec, char *__s);