Home > OS >  I want my zeros to be some other number/character (C )
I want my zeros to be some other number/character (C )

Time:11-20

double floaty=36.6736872;
cout<<fixed<<setprecision(10)<<floaty;

My output is "36.6736872000";

I want my zeros to be some other number.
Eg: If I want zeros to be ^.

then the output should be 36.6736872^^^

I don't have any idea other than using setw and setfill to get my desired output in single line of code

CodePudding user response:

You can use std::ostringstream, and change the resulting string in any way you see fit:

#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>

int main()
{
    double floaty=36.6736872;
    std::ostringstream strm;

    // Get the output as a string  
    strm << std::fixed << std::setprecision(10) << floaty;
    std::string out = strm.str();

    // Process the output
    auto iter = out.rbegin();
    while (iter != out.rend() && *iter == '0')
    {
       *iter = '^';
         iter;
    }
    std::cout << out;
}

Output:

36.6736872^^^
  • Related