Convert a number to a string with specified length in C++

后端 未结 8 1336
情深已故
情深已故 2020-12-04 23:36

I have some numbers of different length (like 1, 999, 76492, so on) and I want to convert them all to strings with a common length (for example, if the length is 6, then tho

相关标签:
8条回答
  • 2020-12-05 00:09

    sprintf is the C-like way of doing this, which also works in C++.

    In C++, a combination of a stringstream and stream output formatting (see http://www.arachnoid.com/cpptutor/student3.html ) will do the job.

    0 讨论(0)
  • 2020-12-05 00:22
    char str[7];
    snprintf (str, 7, "%06d", n);
    

    See snprintf

    0 讨论(0)
提交回复
热议问题