Type Conversion : from QString to float and double
-
I have compiled the following code:
QString myValue = "313.567889"; qDebug()<<" value after converting to float=" << myValue.toFloat(); // Output - 313.568 qDebug()<<" value after converting to double=" << myValue.Double(); // Output - 313.568
As you can see in both of the conversion , I got
313.568
whereas I wanted to get313.567889
. How can this conversion be done? -
I have compiled the following code:
QString myValue = "313.567889"; qDebug()<<" value after converting to float=" << myValue.toFloat(); // Output - 313.568 qDebug()<<" value after converting to double=" << myValue.Double(); // Output - 313.568
As you can see in both of the conversion , I got
313.568
whereas I wanted to get313.567889
. How can this conversion be done?@Swati777999
let me quote my self from one of your previous topics:@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.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.
you do not get a higher precision out of double, if you have 3 places before the decimal point!
0.567889 fits fine into a double, 313.567889 does not.
-
I have compiled the following code:
QString myValue = "313.567889"; qDebug()<<" value after converting to float=" << myValue.toFloat(); // Output - 313.568 qDebug()<<" value after converting to double=" << myValue.Double(); // Output - 313.568
As you can see in both of the conversion , I got
313.568
whereas I wanted to get313.567889
. How can this conversion be done?@Swati777999 said in Type Conversion : from QString to float and double:
I wanted to get
313.567889
It is impossible to store
313.567889
exactly in afloat
ordouble
. Try it: https://baseconvert.com/ieee-754-floating-point -
@Swati777999 said in Type Conversion : from QString to float and double:
I wanted to get
313.567889
It is impossible to store
313.567889
exactly in afloat
ordouble
. Try it: https://baseconvert.com/ieee-754-floating-point@JKSH said in Type Conversion : from QString to float and double:
@Swati777999 said in Type Conversion : from QString to float and double:
I wanted to get
313.567889
It is impossible to store
313.567889
exactly in afloat
ordouble
. Try it: https://baseconvert.com/ieee-754-floating-pointSo how can I store the exact number?
-
@JKSH said in Type Conversion : from QString to float and double:
@Swati777999 said in Type Conversion : from QString to float and double:
I wanted to get
313.567889
It is impossible to store
313.567889
exactly in afloat
ordouble
. Try it: https://baseconvert.com/ieee-754-floating-pointSo how can I store the exact number?
@Swati777999 said in Type Conversion : from QString to float and double:
So how can I store the exact number?
Questions:
- Did you try the link I gave you?
- Why do you need this value to 9 significant digits?
-
@Swati777999 said in Type Conversion : from QString to float and double:
So how can I store the exact number?
Questions:
- Did you try the link I gave you?
- Why do you need this value to 9 significant digits?
@JKSH said in Type Conversion : from QString to float and double:
@Swati777999 said in Type Conversion : from QString to float and double:
So how can I store the exact number?
Questions:
1. Did you try the link I gave you?Yes but there's no scope for conversion from string format [given in the link].
2. Why do you need this value to 9 significant digits?
I want the answer to be shown in a non-string format. So, I want all digits of string to be shown without losing any digit during display.
-
I have compiled the following code:
QString myValue = "313.567889"; qDebug()<<" value after converting to float=" << myValue.toFloat(); // Output - 313.568 qDebug()<<" value after converting to double=" << myValue.Double(); // Output - 313.568
As you can see in both of the conversion , I got
313.568
whereas I wanted to get313.567889
. How can this conversion be done?@Swati777999 said in Type Conversion : from QString to float and double:
As you can see in both of the conversion , I got 313.568 whereas I wanted to get 313.567889. How can this conversion be done?
Please take in consideration the answer you have already got.
real values converted in
float
ordouble
are converted in a specific format (called IEEE 754). So this is an approching value not the exact value. It is not possible with float or double to store a precise value! -
@JKSH said in Type Conversion : from QString to float and double:
@Swati777999 said in Type Conversion : from QString to float and double:
So how can I store the exact number?
Questions:
1. Did you try the link I gave you?Yes but there's no scope for conversion from string format [given in the link].
2. Why do you need this value to 9 significant digits?
I want the answer to be shown in a non-string format. So, I want all digits of string to be shown without losing any digit during display.
@Swati777999 said in Type Conversion : from QString to float and double:
I want the answer to be shown in a non-string format.
If you already have the number as string why do you need to convert it to float/double and then back to show it? Why don't you simply show your string?
-
@Swati777999 said in Type Conversion : from QString to float and double:
I want the answer to be shown in a non-string format.
If you already have the number as string why do you need to convert it to float/double and then back to show it? Why don't you simply show your string?
-
@Swati777999 said in Type Conversion : from QString to float and double:
I want the answer to be shown in a non-string format.
If you already have the number as string why do you need to convert it to float/double and then back to show it? Why don't you simply show your string?
@jsulm
It is required to be shown in a non-string format. -
@jsulm
It is required to be shown in a non-string format.@Swati777999 said in Type Conversion : from QString to float and double:
@jsulm
It is required to be shown in a non-string format.what does that mean ? shown where ?
in anticipating, does this mean "non string format" for you ?
QString myValue = "313.567889"; QDebug debug = qDebug(); debug.noquote(); debug << myValue;
-
@jsulm
It is required to be shown in a non-string format.@Swati777999 said in Type Conversion : from QString to float and double:
It is required to be shown in a non-string format
I don't get it. Everything you see on the screen is shown as string. What do you think qDebug is doing with your number to show it?
-
I have compiled the following code:
QString myValue = "313.567889"; qDebug()<<" value after converting to float=" << myValue.toFloat(); // Output - 313.568 qDebug()<<" value after converting to double=" << myValue.Double(); // Output - 313.568
As you can see in both of the conversion , I got
313.568
whereas I wanted to get313.567889
. How can this conversion be done?@Swati777999 said in Type Conversion : from QString to float and double:
QString myValue = "313.567889";
qDebug()<<" value after converting to float=" << myValue.toFloat(); // Output - 313.568
qDebug()<<" value after converting to double=" << myValue.Double(); // Output - 313.568Strange result from QDebug ...
QString myValue = "313.567889"; qDebug()<<QString::asprintf("float = %f",myValue.toFloat()); qDebug()<<QString::asprintf("double = %f",myValue.toDouble()); "float = 313.567902" "double = 313.567889"
You excedeed the float limits already !
Doubles are better than float but have its own limits also:qDebug()<<QString::asprintf("float : %f %f %f %f",3.140000f,30.140000f,300.140000f,3000.140000f); qDebug()<<QString::asprintf("double : %f %f %f %f",3.140000,3000.140000,0.140000+30e9,0.140000+30e10); "float : 3.140000 30.139999 300.140015 3000.139893" "double : 3.140000 3000.140000 30000000000.139999 300000000000.140015"
Don't try to predict any result from floating point numbers, you will be wrong every time.
-
If you want better precision you might have to use a library. I found this post about boost. So you could possibly use the boost multiprecision library: https://stackoverflow.com/questions/28133826/c-decimal-arithmetic-libraries
-
@Swati777999 said in Type Conversion : from QString to float and double:
I want the answer to be shown in a non-string format. So, I want all digits of string to be shown without losing any digit during display.
You have not explained why you want this.
- It is impossible to do it using
float
anddouble
. - It is possible to do it by using an external library (like @fcarney said), but then you need to write lots of extra code and it is probably not worth it!
So, first you should tell us why "I want the answer to be shown in a non-string format. So, I want all digits of string to be shown without losing any digit during display." Then we can discuss the best solution.
- It is impossible to do it using
-
I'm suprised how many "wrong" answers there are in this thread given that the respondents are generally smart folks. 64 bit ieee double has requisite bits available to represent the requested value to 6 decimal places. That of course does not negate the OPs misunderstanding about float point approximations.
The following program demonstrates that the requested output format is possible, but also insinuates that the default float/double formatting of QDebug isn't adequate to display more decimal places to the right.
#include <iomanip> #include <iostream> #include <sstream> #include <string> template<class T> std::string FORMAT(const T v) { using namespace std; ostringstream out; out << setprecision(10) << v; return out.str(); }; int main(int argc, char* argv[]) { using namespace std; string number = "313.567889"; istringstream floatInput(number); istringstream doubleInput(number); float floatVal = 0.0F; floatInput >> floatVal; double doubleVal = 0.0; doubleInput >> doubleVal; cout << "number: " << number << endl; cout << "float: " << FORMAT<float>(floatVal) << endl; cout << "double: " << FORMAT<double>(doubleVal) << endl; return 0; }
workshop:~$ ./a.out
number: 313.567889
float: 313.5679016
double: 313.567889
workshop:~$