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

两盒软妹~` 提交于 2019-12-11 00:15:11

问题


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


回答1:


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




回答2:


If you want to create following just as qCustomPlot sample set axis properties like codes bellow.

// setup look of bottom tick labels:
customPlot->xAxis->setTickLabelRotation(30);
customPlot->xAxis->ticker()->setTickCount(9);
customPlot->xAxis->setNumberFormat("ebc");
customPlot->xAxis->setNumberPrecision(1);
customPlot->xAxis->moveRange(-10);
// make top right axes clones of bottom left axes. Looks prettier:
customPlot->axisRect()->setupFullAxesBox();

setNumberPrecision sets number of float digits if you set it to zero may show all digits in plot axis.
setNumberFormat("g") also may show all digits too , anyway qCustomplot try to show values alongside axis as beauty can be.



Here is a preview.




回答3:


I'm guessing you want std::setprecision.



来源:https://stackoverflow.com/questions/44999495/display-12digit-double-or-int-as-full-instead-of-3-23223e9-on-qlabel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!