One solution is to convert the value to string and split it by '.' (dot) character:
const float value = 3.141592;
const int precision = 6;
qDebug() << splitted;
// should output ("3","141592"), you can then convert them to integer values with QString::toInt method
const float value = 3.141592;
const int precision = 6;
const QString string = QString::number(value,'g',precision);
const QStringList splitted = string.split('.');
qDebug() << splitted;
// should output ("3","141592"), you can then convert them to integer values with QString::toInt method
To copy to clipboard, switch view to plain text mode
.
Another way is to operate on the float value itself, in order to extract the largest integer not greater than given value - a definition of the "floor" function :
const float value = 3.141592;
const int x = floor(value);
const float y = value-x;
// x == 3;
// y ~= 0.141592
const float value = 3.141592;
const int x = floor(value);
const float y = value-x;
// x == 3;
// y ~= 0.141592
To copy to clipboard, switch view to plain text mode
Bookmarks