[SOLVED] "Round" double
-
Hello guys, i'm with a problem. (I think it's simple)
I'm with a double number: 52.48, and i want to round it to 52.4 only. Without the last number, that in this case is "8".
-
I believe that the "static method number of class QString":http://qt-project.org/doc/qt-5.0/qtcore/qstring.html#number-2 may help you:
@QString::number( 52.48, 'f', 1);@
-
It's returning "52.5", i want to return "52.4".
Understand?
Thanks.
-
[quote author="l3e0wulf" date="1362577800"]It's returning "52.5", i want to return "52.4".
Understand?
Thanks.[/quote]
I don't know a built-in method that will return you this results but you can easily write your own method. Covert the double to string and remove the unnecessary digits from its end.
-
[quote author="l3e0wulf" date="1362577800"]It's returning "52.5", i want to return "52.4".
Understand?
Thanks.[/quote]
What I can say is that the principle of "Rounding" won't apply here.
Maybe you can convert your number to QString using QString::number() then use QRegExp rx("(\d+.\d)") or something similar.
-
Look my problem.
I have two double.
One: 52.48
Two: 52.4I want to know when the "Two - 52.4" number is like the "One - 52.48", and I realised the "One" is always one number different! The last number.
When I put QString::number("52.48", 'f', 1); it's return 52.5 and in "52.4". It's return: "52.4".
-
[quote author="l3e0wulf" date="1362579716"]
When I put QString::number("52.48", 'f', 1); it's return 52.5 and in "52.4". It's return: "52.4".[/quote]Because you need to put "2" in the third parameter. Then remove the last number at the end.
@ QString::number("52.48", 'f', 2);@The third parameter is the "precision", using 3 or more will automatically append 0's, AFAIK.
@ QString::number("52.48", 'f', 4); //it will result to "52.4800"@ -
I think one of the problems is that 52.48 is never like 52.4, if you round it, it's (always) 52.5, so I don't think you'll find a way to solve your problem using numeric types.
I agree with Leon and Core_ReaQtor you can use QString to transform your number, first passing double to QString, transforming it and then passing it to double again.
It's a little "cutre" as we say in Spain (I think it's stingy in english), but I don't have a better idea.Regards!!
-
When i put QString::number("52.4", 'f', 2); i'ts return: "52.40"
When i put QString::number("52.48", 'f', 2); i'ts return: "52.48"When i put QString::number("52.4", 'f', 1); i'ts return: "52.4"
When i put QString::number("52.48", 'f', 1); i'ts return: "52.5"When i put QString::number("52.4", 'f', 2); i'ts return: "52.40"
When i put QString::number("52.48", 'f', 1); i'ts return: "52.5"When i put QString::number("52.4", 'f', 1); i'ts return: "52.4"
When i put QString::number("52.48", 'f', 2); i'ts return: "52.48" -
BelenMuñoz:
bq. I agree with Leon and Core_ReaQtor you can use QString to transform your number, first passing double to QString, transforming it and then passing it to double again.
I'll try.
-
I used QString::mid!
@qDebug() << QString::number(52.48).mid(QString::number(52.48).length() - 1, QString::number(52.48).length());@
And it's return the last number.
-
Update
Maybe a little big, but that's the solution:
@qDebug() << QString::number(52.48).replace(QString::number(52.48).mid(QString::number(52.48).length() - 1, QString::number(52.48).length()), "");@
-
Keep don't working.
The problem now is: 52.48 now is 5.4 with:
qDebug() << QString::number(52.48).replace(QString::number(52.48).mid(QString::number(52.48).length() - 1, QString::number(52.48).length()), "");
-
Hi!
Why are you converting to string?
Multiply both numbers by the number of decimals and cast to int.
So
@auto a = (int)(52.4 * 10) // 524
auto b = (int)(52.48 * 10) // 524if( a == b) // means that 52.4 equals 52.48
...
@Further on, you can replace * 10 with pow(10, decimalPlaces) to have decimal place comparison dynamical.
If you want it back written as 52.4, just divide it by 10.
Regards,
Jake -
Wow, wow, wow, Thank you very much Jake007!
-
You're welcome :) .
Don't forget to mark thread as solved.
And next time you're dealing only with numbers, please leave strings out of it.
Your processor will be grateful :)Regards,
Jake