Closed. This question is off-topic. It is not currently accepting answers. Learn more
想改进这个问题吗?Update the question所以堆栈溢出的值小于aa>。
6年前关闭。
我有一个包含浮点数的变量,如何将其添加到字符串中?
像这样的:
int main() {
    char postdata[] = "field1=";
    float mynumber = 123.12;
    postdata = postdata + mynumber;
    return 0;
}

我希望最终结果是“field1=123.1”
似乎不像postdata+mynumber那么简单:(

最佳答案

使用sprintf

char buffer[32];
sprintf(buffer, "%s%f", postdata, mynumber);

如果需要一位数精度:
char buffer[32];
sprintf(buffer, "%s%.1f", postdata, mynumber);

Here是一个有效的例子。

关于c - C-如何将float变量添加到字符串,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18486485/

10-11 19:06