C++: Formatted Numeric Output


Express a number in decimal as a fixed-length string with leading zeros.

For example, the number 7.125 could be expressed as “00007.125”.

#include <iostream>
#include <iomanip>

int main()
	std::cout << std::setfill('0') << std::setw(9) << std::fixed << std::setprecision(3) << 7.125 << std::endl;
	return 0;


Content is available under GNU Free Documentation License 1.2.

Subscribe to TFE Times

Enter your email address to become a member of TFE Times today!