如何使用"%f"将double值填充到具有正确精度的字符串中

AMM*_*AMM 6 c floating-point precision printf

我试图用这样的方法填充一个带有double值的字符串sprintf:

sprintf(S, "%f", val);
Run Code Online (Sandbox Code Playgroud)

但精度正在被削减到小数点后六位.我需要大约10个小数位才能达到精度.

怎么能实现呢?

Agn*_*ian 9

%[宽度].[精度]

宽度应包括小数点.

%8.2表示8个字符宽; 点前5位和后2位.该点保留一个字符.

5 + 1 + 2 = 8


Yan*_*min 7

你想要的是一个修饰符:

sprintf(S, "%.10f", val);
Run Code Online (Sandbox Code Playgroud)

man sprintf将有更多关于格式说明符的细节.