The question is simple. I have messed up with std::fixed , std::setprecicion , std::setw , but none of them solves my problem.
If I have a integer variable equal to 1, I want it to be printed as: 1000
Thank you.
I think you mean you want it to print as 0001, not as 1000.
Look at printf / sprintf with a format string. For example:
#include <iostream>
int main(int argc, char **argv) {
int i = 1;
printf("%04d\n", i);
}
The question is not very clear on what you are trying to achieve.
If you want to print the value of your variable with 3 zeroes appended to it, you should just print the variable and then 3 zeroes.
cout << x << "000" << endl;
If you need to do any computation with it, you could always just multiply it by 1000
.
int y = x * 1000;
cout << y << endl;
Keep in mind that this may cause an overflow, though...
You can use std::left
together with std::setw
and std::setfill
to add a number of specific fill characters on the right. Example with std::cout
:
#include <iostream>
#include <iomanip>
#include <ios>
int main() {
std::cout << std::setw(4) << std::setfill('0') << std::left << 1;
return 0;
}
I believe it should do the job.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.