diff --git a/lib_standard_app/format.c b/lib_standard_app/format.c index 9c889f6ee..a38d5e4e8 100644 --- a/lib_standard_app/format.c +++ b/lib_standard_app/format.c @@ -129,6 +129,24 @@ bool format_fpu64(char *dst, size_t dst_len, const uint64_t value, uint8_t decim return true; } +bool format_fpu64_trimmed(char *dst, size_t dst_len, const uint64_t value, uint8_t decimals) { + if (!format_fpu64(dst, dst_len, value, decimals)) { + return false; + } + + size_t len = strlen(dst); + + while (len > 0 && (dst[len - 1] == '0' || dst[len - 1] == '.')) { + if (dst[len - 1] == '.') { + dst[len - 1] = '\0'; + return true; + } + len--; + } + dst[len] = '\0'; + return true; +} + int format_hex(const uint8_t *in, size_t in_len, char *out, size_t out_len) { if (out_len < 2 * in_len + 1) { return -1; diff --git a/lib_standard_app/format.h b/lib_standard_app/format.h index f1457483a..00242d53b 100644 --- a/lib_standard_app/format.h +++ b/lib_standard_app/format.h @@ -51,6 +51,23 @@ bool format_u64(char *dst, size_t dst_len, uint64_t value); */ bool format_fpu64(char *dst, size_t dst_len, const uint64_t value, uint8_t decimals); +/** + * Format 64-bit unsigned integer as string with decimals and trimmed zeros and dot. + * + * @param[out] dst + * Pointer to output string. + * @param[in] dst_len + * Length of output string. + * @param[in] value + * 64-bit unsigned integer to format. + * @param[in] decimals + * Number of digits after decimal separator. + * + * @return true if success, false otherwise. + * + */ +bool format_fpu64_trimmed(char *dst, size_t dst_len, const uint64_t value, uint8_t decimals); + /** * Format byte buffer to uppercase hexadecimal string. *