How to left pad a number with zeros in C++

1 Answer

0 votes
#include <iostream>
#include <string>

std::string left_pad_with_zeros(int n) {
    std::string result = std::to_string(n);
    
    while (result.length() < 8) {
        result = "0" + result;
    }
    
    return result;
}
int main() {
    int n = 17493;
    
    std::string result = left_pad_with_zeros(n);
    
    std::cout << result << std::endl;
}

 
 
/*
run:
 
00017493
 
*/

 



answered May 29, 2024 by avibootz

Related questions

1 answer 129 views
129 views asked May 29, 2024 by avibootz
2 answers 177 views
2 answers 151 views
1 answer 196 views
196 views asked Apr 28, 2016 by avibootz
3 answers 282 views
2 answers 159 views
1 answer 200 views
...