Home > Mobile >  Printing % character attached to floating number
Printing % character attached to floating number

Time:12-11

So Let's say I am overloading OS operator and I want to have some sort of table of values divided into columns looking like this:

┌───────────────┬──────────────────────┬─────────────┬──────────────┐
│      X        │         Y.           │ Z.          │ value        │
├───────────────┼──────────────────────┼─────────────┼──────────────┤

and below I am printing those Values using stream:

<< std::setw(13) << std::noshowpoint << value << "%" << "|" << endl;

how can I do this so the output of let's say value equal to 5 will be printed as 5%

I tried to use to_string method but results were overriding noshowpoint operator.

CodePudding user response:

First create the string for the value and percent sign.

Then use that string with the formatting.

You can do the first part using a string output stream. And you can combine it into a single statement:

std::cout << std::setw(13)
          << (std::ostringstream{ } << std::noshowpoint << value << '%').str()
          << "|\n";
  • Related