0

I trying to make an ostream function that return an concatenation of an char and a number. I want the number to be with precision 2: let say "20.1234" so I want to print:"20.12", or if the number is "10" print "10.00". This is my function:

std::ostream& operator<< (std::ostream& out,
        const Assig& assignment)
{
        for (int i=rows-1; i>=0; i--)
        {
            out<<char(assig.arr[i][1])<<" = "<<setprecision(2)<<assig.arr[i][0];
                out<<", ";
        }
    return out;
}

but it print "a = 10" instead of "a =10.00". Maybe I need to use ostringstream? But I can't change the name of the function: "std::ostream& operator<< (std::ostream& out,const Assignment& assignment)"

  • what you marked as duplicate doesn't work!! – asdas asda Jun 11 '15 at 10:49
  • and this is very different question – asdas asda Jun 11 '15 at 10:54
  • *"This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question."* The answer in the dupe explains how to print floating point numbers with exactly two digits after the decimal point, if that is not what you need, you need to be more specific *in your new question*. – Baum mit Augen Jun 11 '15 at 11:12

0 Answers0