我正在尝试在C中使用12位小数的精度。我不知道是否有更简单的解决方案。但至少该代码有效。现在我只想将结果保存为“long double”,但“strtold()”无法正常工作
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;
}
答案 0 :(得分:2)
printf("%.12lf",num);
解决了这个问题。
将两个双倍相乘并像这样打印。无需使用long
。