Skip to content

Commit

Permalink
fix rounding of web number-inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelDvP committed Jan 18, 2023
1 parent a21352a commit 8db5724
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,11 @@ char * Helpers::render_value(char * result, const double value, const int8_t for
return nullptr;
}

uint32_t p[] = {0, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000};
uint32_t p[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000};

char * ret = result;
auto whole = (int32_t)value;
double v = value < 0 ? value - 1.0 / (2 * p[format]) : value + 1.0 / (2 * p[format]);
auto whole = (int32_t)v;

itoa(whole, result, 10);

Expand All @@ -261,7 +262,7 @@ char * Helpers::render_value(char * result, const double value, const int8_t for
}

*result++ = '.';
auto decimal = abs((int32_t)((value - whole) * p[format]));
auto decimal = abs((int32_t)((v - whole) * p[format]));
for (int8_t i = 1; i < format; i++) {
if (decimal < p[i]) {
*result++ = '0'; // add leading zeros
Expand Down

0 comments on commit 8db5724

Please sign in to comment.