Can we get underscores in printf instead of blank spaces while writing “%15.2f” or something like that?

#include <iostream>
using namespace std;

int main()
{
    double B = 2006.008;
    printf("%15.2f", B);  

    return 0;
}

Output:

   +2006.01

with blank spaces in the beginning

I want to replace those white spaces with underscores(_). Is there any syntax in print or any direct method to do that?

My desired output:

_______+2006.01

Answer

As in the comments suggested you can write a function for this, but not with printf().

#include <algorithm>
#include <iostream>

std::string formatNumber(double number)
{
    char buffer[15 + 1 + 2];
    sprintf(buffer, "%15.2f", number);  

    std::string s(buffer);
    std::replace(s.begin(), s.end(), ' ', '_');

    return s;
}

int main()
{
    double B = 2006.008;
    auto toPrint = formatNumber(B);

    std::cout << toPrint << std::endl;

    return 0;
}