Bryan Romero
Bryan Romero

Reputation: 174

Printing a double with plus or minus sign with a QString

I want to print a double with a QString.

Example if the double is -45, it has to print -045.0, if the value is +45 it has to print +045.0.

I tried using sprintf using the format "%05.1f" but when the double changes value I get printed : +-45.0.

My function:

Function(double value)
{

QString string;


 if (value > 0)
    {
        string.sprintf("+%05.1f", value);
    }else
{
string.sprintf("%05.1f", value);
}

 }

Upvotes: 2

Views: 699

Answers (1)

Nicolas Dusart
Nicolas Dusart

Reputation: 2007

The format to display the '+' sign in printf is %+...

So your function would be:

void Function(double value)
{
  QString string;
  string.sprintf("%+06.1f", value);
 }

Upvotes: 3

Related Questions