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

3

2

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

Attitudinarian answered 9/7, 2017 at 18:7 Comment(2)
Possible duplicate of How do I print a double value with full precision using cout?Jabez
I am not printing it. Using cout is using stream. In my case it's not stream related. I have already checked that post before I posted this. Thank you.Attitudinarian
T
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

Testate answered 10/7, 2017 at 14:17 Comment(1)
Thank you so much, solve my entire issue. You saved my day.Attitudinarian
C
3

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.

enter image description here

Crimea answered 10/7, 2017 at 5:5 Comment(0)
H
2

I'm guessing you want std::setprecision.

Heathcote answered 9/7, 2017 at 18:10 Comment(1)
Thanks for instant reply, checked the doc and it's for stream use only. How if I want to set it like this ---- ui->label_xcoord_1->setNum(x3); Tried this ---- ui->label_xcoord_1->setNum(std::setprecision(12)<<x3) but it didn't workAttitudinarian
T
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

Testate answered 10/7, 2017 at 14:17 Comment(1)
Thank you so much, solve my entire issue. You saved my day.Attitudinarian

© 2022 - 2024 — McMap. All rights reserved.