Search code examples
c++qtnumbersqcustomplot

Display 12digit double or int as full instead of 3.23223e+9 on QLabel


C++ is displaying a number 3232235975 on QLabel as 3.23223e+9, while on QCustomPlot axis as 3.23223*10^9. I am not involving stream such as std::cout, that is why std::setprecision doesn't work for my case.

Am actually working with QCustomPlot to plot graph with 12digit numbers on the axis. Is this C++ issue or is it QCustomPlot mechanism issue? How can I display all the digits of the number?

Thanks

EDIT: After receiving hint of setNumberPrecision() from @saeed, now the coordination is still showing in 2.88798e+9 format. Now that the axis is already showing 4000000000, I want the coordination QLabel (shown in the image I attached) also display 2887984335.

I am getting the coordination and setting it to a QLabel like this.

double x2 = ui->widget_graph2->xAxis->pixelToCoord(_mouseEvent->pos().x());
double y2 = ui->widget_graph2->yAxis-pixelToCoord(_mouseEvent->pos().y());
ui->label_xcoord_2->setNum(x2);
ui->label_ycoord_2->setNum(y2);

And I'm setting the plot data using setData() like this

QVector<double> x2(i), y2(i);
for(int o = 0; o <= i; o++){
    double dSec = arrayIPxTime[o][0] - startSecond;
    double dMin = dSec/60;
    double ipv4addr = arrayIPxTime[o][1];
    x2[o] = dMin;
    y2[o] = ipv4addr;
}
ui->widget_graph2->addGraph(0);
ui->widget_graph2->graph(0)->setData(x2, y2);
ui->widget_graph2->installEventFilter(this);

Working with QCustomPlot


Solution

  • To display coordinates in your wanted format

    ui->label_xcoord_2->setNum(x2);
    ui->label_ycoord_2->setNum(y2);
    

    Use void setText(const QString &) method for QLabel and pass

    QString QString::number(double n, char format = 'g', int precision = 6)
    

    As parameter with your wanted format and precision:

    Format Meaning

    e format as [-]9.9e[+|-]999

    E format as [-]9.9E[+|-]999

    f format as [-]9.9

    g use e or f format, whichever is the most concise

    G use E or f format, whichever is the most concise