Rounding a real number to nth place of decimal
-
Hi all,
I want to know the syntax for rounding a real number to nth decimal place.
Thanks in advance!
-
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);
-
@Swati777999 said in Rounding a float value to nth place of decimal:
I want to know the syntax for rounding a real number to nth decimal place.
What do you mean with this?
Do you want to create a string representation of the real number? -
@KroMignon
suppose I want to round pi to 3 decimal places or 4 decimal places. How can I achieve it in qt? -
@Swati777999 said in Rounding a float value to nth place of decimal:
suppose I want to round pi to 3 decimal places or 4 decimal places. How can I achieve it in qt?
This do not reply to my question, what is the purpose of this question?
-
@KroMignon said in Rounding a float value to nth place of decimal:
@Swati777999 said in Rounding a float value to nth place of decimal:
suppose I want to round pi to 3 decimal places or 4 decimal places. How can I achieve it in qt?
This do not reply to my question, what is the purpose of this question?
What's the equivalent code in Qt for round(pi ,3) where pi =3.1415..... and round(real value,number of decimal places rounded to ) ?
-
@Swati777999 said in Rounding a float value to nth place of decimal:
What's the equivalent code in Qt for round(pi ,3) where pi =3.1415..... and round(real value,number of decimal places rounded to ) ?
This question don't made sense to me. What
round()
function are you talking about? In which programming language?
Again, why do you want to round the real value? What is the use case?
Do you want to create a string which contains the rounded value? -
@KroMignon said in Rounding a float value to nth place of decimal:
@Swati777999 said in Rounding a float value to nth place of decimal:
What's the equivalent code in Qt for round(pi ,3) where pi =3.1415..... and round(real value,number of decimal places rounded to ) ?
This question don't made sense to me. What
round()
function are you talking about? In which programming language?round()
of Python.Again, why do you want to round the real value? What is the use case?
I want to round the real value to specific decimal places because I don't want to store other digits in my database.
Use cases can be any real number like round(54.3467,2) = 54.35 , round(5.842) = 6 , round(8.54123,4)=8.5412 ..so onDo you want to create a string which contains the rounded value?
Yes. It does not matter which format but I want to be able to see the correct result on my screen.
-
@Swati777999 said in Rounding a float value 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.
Use cases can be any real number like round(54.3467,2) = 54.35 , round(5.842) = 6 , round(8.54123,4)=8.5412 ..so onThis question still looks strange and I am not sure you are really understanding what you are doing.
If you only what to read the value in your DB rounded to a specific decimal, why not using the SQLROUND()
function?SELECT ROUND(floatValue, 2) as RoundedValue from myTable;
-
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);
-
@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.
1/23