c++ - sprintf 和 snprintf 无法封装 double

标签 c++ printf

sprintf 和 snprintf 都去掉了我的小数点的最后两位。它无法封装我的双重值(value)。

代码:

int main(void)
{
        char command[70];
        char sendbuy[12];
        double biggestMark2;
        biggestMark2 = 0.00550006;
        sprintf(sendbuy,"%lf",biggestMark2);
        //snprintf(command, sizeof command, "php oin.php 155  %s", sendbuy);
        sprintf(command, "php oin.php 155 %s",sendbuy);
        cout << command << endl;
        cout << biggestMark2 << endl;
}

输出:

 # ./veryTemp.o
php oin.php 155 0.005500
0.00550006

预期输出:

 # ./veryTemp.o
php oin.php 155 0.00550006
0.00550006

如何将我的所有数字放入我的命令变量中?

最佳答案

打印 float 的默认精度为 6,如标准所述:

7.21.6.1 p8:

f,F

A double argument representing a floating-point number is converted to decimal notation in the style [−]ddd.ddd, where the number of digits after the decimal-point character is equal to the precision specification. If the precision is missing, it is taken as 6; if the precision is zero and the # flag is not specified, no decimal-point character appears. If a decimal-point character appears, at least one digit appears before it. The value is rounded to the appropriate number of digits.

关于c++ - sprintf 和 snprintf 无法封装 double,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27154738/

相关文章:

c++ - 线程 : Segmentation fault error

bash - shell 脚本中的 SPRINTF?

c - 打印出十六进制奇怪输出的值?

c - SPARC 程序集参数中的 printf 格式说明符?

c++ - 如果实数可以用 double 表示,我如何编写返回 true 的 C++ 函数?

c# - 如何从 C# 代码在 Visual Studio 中创建 native DLL?

cv::Mat 返回 vector 的 C++ 线程异步 <rectangle>

c++ - 360 游戏 Controller Linux 设备驱动程序问题调用我的探测功能

r - 列出 sprintf 的输入(绕过省略号 ... 函数)

c - vsnprintf_s 是已弃用的 vsnprintf 的适当替代品吗?