How multiply with 12 decimals in C?

92 Views Asked by At

I'm trying to have precision of 12 decimals in C. I don't know if there's an easier solution. But at least that code works. Now I'm just trying to save the result in a "long double" but "strtold()" is not working

char* multiply12Decimals(float n1, float n2)
{
    long n1Digits;
    sscanf(doubleToVoidPointerInNewMemoryLocation(n1*1000000), "%ld", &n1Digits);
    printf("n1Digits: %ld\n", n1Digits);

    long n2Digits;
    sscanf(doubleToVoidPointerInNewMemoryLocation(n2*1000000), "%ld", &n2Digits);
    printf("n2Digits: %ld\n", n2Digits);

    long long mult = (long long) n1Digits*n2Digits;
    printf("mult: %lld\n", mult);

    char *charNum = malloc(30*sizeof(char));

    sprintf (charNum, "0.%012lld\n", mult);
    printf("result: %s\n", charNum);

    return charNum;
}
1

There are 1 best solutions below

1
On BEST ANSWER

printf("%.12lf",num); solves the problem.

Multiply two double and print it like this. No need to use long.