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

后端 未结 8 1359
情深已故
情深已故 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:04

    There are many ways of doing this. The simplest would be:

    int n = 999;
    char buffer[256]; sprintf(buffer, "%06d", n);
    string str(buffer);
    

提交回复
热议问题