Skip to content

Commit

Permalink
Add string_strip_trailing_zeros
Browse files Browse the repository at this point in the history
  • Loading branch information
luboslenco committed Aug 24, 2024
1 parent 186ffdb commit 16d6ff3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
15 changes: 13 additions & 2 deletions sources/iron_string.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,23 @@ char *i32_to_string_hex(int32_t i) {
}

char *f32_to_string(float f) {
int l = snprintf(NULL, 0, "%f", f);
int l = snprintf(NULL, 0, "%.2f", f);
char *r = gc_alloc(l + 1);
sprintf(r, "%f", f);
sprintf(r, "%.2f", f);
string_strip_trailing_zeros(r);
return r;
}

void string_strip_trailing_zeros(char *str) {
int len = strlen(str);
while (str[--len] == '0') {
str[len] = '\0';
}
if (str[len] == '.') {
str[len] = '\0';
}
}

int32_t string_index_of_pos(char *s, char *search, int pos) {
char *found = strstr(s + pos, search);
if (found != NULL) {
Expand Down
1 change: 1 addition & 0 deletions sources/iron_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ char *i32_to_string(int32_t i);
char *i32_to_string_hex(int32_t i);
char *f32_to_string(float f);

void string_strip_trailing_zeros(char *str);
int32_t string_index_of(char *s, char *search);
int32_t string_index_of_pos(char *s, char *search, int pos);
int32_t string_last_index_of(char *s, char *search);
Expand Down
14 changes: 2 additions & 12 deletions sources/zui.c
Original file line number Diff line number Diff line change
Expand Up @@ -2038,16 +2038,6 @@ int zui_combo(zui_handle_t *handle, char_ptr_array_t *texts, char *label, bool s
return handle->position;
}

static void strip_trailing_zeros(char *str) {
int len = strlen(str);
while (str[--len] == '0') {
str[len] = '\0';
}
if (str[len] == '.') {
str[len] = '\0';
}
}

float zui_slider(zui_handle_t *handle, char *text, float from, float to, bool filled, float precision, bool display_value, int align, bool text_edit) {
static char temp[1024];
if (!zui_is_visible(ZUI_ELEMENT_H())) {
Expand Down Expand Up @@ -2092,7 +2082,7 @@ float zui_slider(zui_handle_t *handle, char *text, float from, float to, bool fi
char tmp[256];
sprintf(tmp, "%.2f", handle->value);
handle->text = string_copy(tmp);
strip_trailing_zeros(handle->text);
string_strip_trailing_zeros(handle->text);
zui_start_text_edit(handle, ZUI_ALIGN_LEFT);
handle->changed = current->changed = true;
}
Expand All @@ -2117,7 +2107,7 @@ float zui_slider(zui_handle_t *handle, char *text, float from, float to, bool fi
arm_g2_set_color(current->ops->theme->TEXT_COL); // Value
if (current->text_selected_handle != handle) {
sprintf(temp, "%.2f", round(handle->value * precision) / precision);
strip_trailing_zeros(temp);
string_strip_trailing_zeros(temp);
zui_draw_string(temp, current->ops->theme->TEXT_OFFSET, 0, lalign, true);
}
else {
Expand Down

0 comments on commit 16d6ff3

Please sign in to comment.