#include <iostream>
#include <iomanip>
#include <sstream>
#include <vector>

int main()
{
    std::vector<double> numbers {
    	3.141516, 1.01, 200.78901, 0.12345, 9.99999
    };

    for (auto x : numbers)
    {
    	// "print" the number
    	std::stringstream ss;
    	ss << std::fixed << std::setprecision(4) << x;
    	
    	// remove the unwanted zeroes
    	std::string result{ss.str()};
    	while (result.back() == '0')
    		result.pop_back();
    		
    	// remove the separator if needed	
    	if (result.back() == '.')
    		result.pop_back();
    		
        std::cout << result  << '\n';
    }
    
    std::cout << "\nJustified:\n";
    for (auto x : numbers)
    {
    	// "print" the number
    	std::stringstream ss;
    	ss << std::fixed << std::setprecision(4) << std::setw(15) << x;
    	
    	// remove the unwanted zeroes
    	std::string result{ss.str()};
    	auto it = result.rbegin();
    	while (*it == '0')
    		*it++ = ' ';
    		
    	// remove the separator if needed	
    	if (*it == '.')
    		*it = ' ';
    		
        std::cout << result  << '\n';
    }
}