1

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);
}

 }
Bryan Romero
  • 164
  • 2
  • 11
  • 2
    How did you get +-24.0? What was the value then? – Minh Jan 21 '21 at 12:09
  • The second answer in this [question](https://stackoverflow.com/questions/7234824/format-a-number-to-a-specific-qstring-format) shows a way to achieve that without using a deprecated function. It's not pretty though. – Minh Jan 21 '21 at 12:29

1 Answers1

3

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);
 }
Nicolas Dusart
  • 1,867
  • 18
  • 26