Rounding a real number to nth place of decimal
-
I am looking for the logic of coding in qt not in SQL or Python. I've used the above examples just to show the desired output.
-
@Swati777999 said in Rounding a float value to nth place of decimal:
I am looking for the logic of coding in qt not in SQL or Python. I've used the above examples just to show the desired output.
Sorry, but I am not the right personne to help you. I do not understand what you expect to have.
A float/double value in C/C++ will always be stored in mantis / exposant format (cf. https://en.wikipedia.org/wiki/Double-precision_floating-point_format), so rounding a float do not make sense to me.Perhaps someone else could better understand what you want to do.
-
@KroMignon said in Rounding a float value to nth place of decimal:
@Swati777999 said in Rounding a float value to nth place of decimal:
I am looking for the logic of coding in qt not in SQL or Python. I've used the above examples just to show the desired output.
Sorry, but I am not the right personne to help you. I do not understand what you expect to have.
A float/double value in C/C++ will always be stored in mantis / exposant format (cf. https://en.wikipedia.org/wiki/Double-precision_floating-point_format), so rounding a float do not make sense to me.To be more clear, I have edited the heading of the question. I want to round a real number to the desired place of decimal.
-
Hello, @Swati777999!
Please check out the
qRound
methods: https://doc.qt.io/qt-5/qtglobal.html#qRound
Also, feel free to check out C++ math functionsceil
(rounds up) andfloor
(rounds down): https://stackoverflow.com/questions/19985397/qt-decimals-and-round-up-to-whole-numbers
I think, it could be useful in your case. Happy coding! -
@Swati777999 said in Rounding a real number to nth place of decimal:
To be more clear, I have edited the heading of the question. I want to round a real number to the desired place of decimal.
Again, this not clear because it do not made sense: float/double value are coded as fractional part + exponent is a 32 bit word for float and 64 bit word for double.
You cannot round up a float to another float at nth decimal, this is non sense. -
@Swati777999 said in Rounding a real number to nth place of decimal:
I want to round the real value to specific decimal places because I don't want to store other digits in my database.
Real/floating point numbers (e.g.
float
in Python) do not "store digits" for a number. Rather they store a fixed-size bit pattern (say 8 bytes) which approximates to the variety of real numbers which can actually be stored.Some databases have a
DECIMAL
type which does store numbers with a fixed number of decimal places.You would have to start by stating what your database is. And even then it may not be possible/easy to store exactly as you state, depending on the library you are using to interface with the database.
-
it's rather pointless, but here you go
double roundToNthPlace(double value, int n){ if (n < 1) return static_cast<double>(qRound64(value)); value *= 10 * n; value = qRound(value); value /= 10 * n; return value; }
if you're fine with truncating instead of "rounding", than I would suggest using
https://doc.qt.io/qt-5/qstring.html#number-6
or
https://doc.qt.io/qt-5/qstring.html#arg-9just before/for displaying the value.
-
This is the code I wrote with Qt functions
QString myStr = "314.19"; QString myStr1 = "314.1945327743682"; int dec_pl=6; qDebug() << QString::number(1.125, 'f', 2); qDebug() << "Rounding 314.19 to 3 decimal places = " << QString::number(myStr.toDouble(), 'f', 3); qDebug() << "Rounding 314.1945327743682 to 6 decimal places = " << QString::number(myStr.toDouble(), 'f',dec_pl);
Why is the third result incorrect?-----------------------------------------------------------------------------------------------------------
Edit:
the third result is incorrect as I wroteqDebug() << "Rounding 314.1945327743682 to 6 decimal places = " << QString::number(myStr.toDouble(), 'f',dec_pl);
so I was doing operation on
myStr
instead ofmyStr1
So the correction is
QString myStr = "314.19"; QString myStr1 = "314.1945327743682"; int dec_pl=6; qDebug() << QString::number(1.125, 'f', 2); qDebug() << "Rounding 314.19 to 3 decimal places = " << QString::number(myStr.toDouble(), 'f', 3); qDebug() << "Rounding 314.1945327743682 to 6 decimal places = " << QString::number(myStr1.toDouble(), 'f', dec_pl);
which yields following result
-
@J-Hilk said in Rounding a real number to nth place of decimal:
it's rather pointless, but here you go
double roundToNthPlace(double value, int n){ if (n < 1) return static_cast<double>(qRound64(value)); value *= 10 * n; value = qRound(value); value /= 10 * n; return value; }
if you're fine with truncating instead of "rounding", than I would suggest using
https://doc.qt.io/qt-5/qstring.html#number-6
or
https://doc.qt.io/qt-5/qstring.html#arg-9just before/for displaying the value.
my
value
is inQString
. When I convertedvalue
fromQString
todouble
using.toDouble()
, it's giving incorrect result. -
@Swati777999 said in Rounding a real number to nth place of decimal:
314.1945327743682
it's not wrong, it's the closest representation of your decimal value that is possible with a double.
It's the nature of computer science, you'll have to find a way fitting for your situation that deals/works around it.
-
@J-Hilk said in Rounding a real number to nth place of decimal:
@Swati777999 said in Rounding a real number to nth place of decimal:
314.1945327743682But I 'm expecting to get the result of 314.194533. So, it means there are no functions present in Qt which would round to the given decimal digits as required. I've to write a program for that.
it's not wrong, it's the closest representation of your decimal value that is possible with a double.
It's the nature of computer science, you'll have to find a way fitting for your situation that deals/works around it.
-
@Swati777999 said in Rounding a real number to nth place of decimal:
Why is the third result incorrect?
In the code you posted you are "rounding"
myStr
to 6 decimal places instead ofmyStr1
. So, the computer does exactly what you ask it to do.BTW, "rounding" is something different than specifying the number of decimal places for output. The saved floating point number still has the same precision as before.
-
@SimonSchroeder nice catch, that explains why it doesn't collapse down to
314.195
but to314.19
-
@SimonSchroeder
Nice observation , Simon! This is where I made the mistake. Now, I'm getting values as 314.190 ,314.194533 which are the expected values.Thanks for pointing it out,otherwise I would've continued to question my own coding ability.
-
This post is deleted!