X-Git-Url: http://sigrok.org/gitweb/?a=blobdiff_plain;f=src%2Fanalog.c;h=ccf6d31934bf605cc4a300d989b83055f9ceaa7d;hb=c2a25ebb8fbf992cdf9fee96d44f1c27f1f48f51;hp=9def3ac8fa62347f405d2acabf2d59b98c6eb4e8;hpb=1954dfa96310e286bea6b4d55629ba08cee2f417;p=libsigrok.git diff --git a/src/analog.c b/src/analog.c index 9def3ac8..ccf6d319 100644 --- a/src/analog.c +++ b/src/analog.c @@ -17,7 +17,10 @@ * along with this program. If not, see . */ +#include +#include #include +#include #include "libsigrok.h" #include "libsigrok-internal.h" @@ -98,3 +101,36 @@ SR_API int sr_analog_to_float(const struct sr_datafeed_analog2 *analog, return SR_OK; } + +/* + * Convert a floating point value to a string, limited to the given + * number of decimal digits. + * + * @param value The value to convert. + * @param digits Number of digits after the decimal point to print. + * @param outbuf Buffer in which the resulting string will be placed. + * @param bufsize Size of the buffer in bytes. + * + * @retval SR_OK + * + * @since 0.4.0 + */ +SR_API int sr_analog_float_to_string(float value, int digits, char *outbuf, + int bufsize) +{ + int cnt, i; + + /* This produces at least one too many digits */ + snprintf(outbuf, bufsize, "%.*f", digits, value); + for (i = 0, cnt = 0; outbuf[i] && i < bufsize; i++) { + if (isdigit(outbuf[i++])) + cnt++; + if (cnt == digits) { + outbuf[i] = 0; + break; + } + } + + return SR_OK; +} +