Skip to content

Commit

Permalink
format.h: add format_fpu64_trimmed
Browse files Browse the repository at this point in the history
(cherry picked from commit 9e3b19a)
  • Loading branch information
sgliner-ledger authored and Xavier Chapron committed Aug 18, 2023
1 parent bad36e6 commit da768fb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib_standard_app/format.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
17 changes: 17 additions & 0 deletions lib_standard_app/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down

0 comments on commit da768fb

Please sign in to comment.