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/