~dheinemann/Replicalc

188bf7d306bdc4291d46e54379b9ae76a25824e6 — David Heinemann 7 months ago ee7b54c
Trim trailing zeros and period from decimal numbers
1 files changed, 22 insertions(+), 1 deletions(-)

M src/main.c
M src/main.c => src/main.c +22 -1
@@ 46,9 46,29 @@ char* trim(char* str) {
    return str;
}

/*
 * Trim trailing zeros and period from decimal numbers.
 */
void format(double value, char* output) {
    int i;

    sprintf(output, "%f", value);
    for (i = (int) strlen(output) - 1; i >= 0; i--) {
        if (output[i] == '0') {
            output[i] = '\0';
        } else {
            if (output[i] == '.') {
                output[i] = '\0';
            }
            break;
        }
    }
}

int main() {
    double result;
    char expr[EXPR_LEN];
    char resultStr[EXPR_LEN];
    while (1) {
        printf("> ");
        fgets(expr, EXPR_LEN, stdin);


@@ 59,7 79,8 @@ int main() {
        result = calculate(expr, &errorCode);
        switch (errorCode) {
            case Error_Success:
                printf("%f\n", result);
                format(result, resultStr);
                printf("%s\n", resultStr);
                break;
            case Error_UnbalParens:
                printf("Error: Unbalanced parentheses\n");