How to get rid of scientific notation while converting from QString to float
-
wrote on 3 Mar 2022, 03:42 last edited by
Here's my code
QString myStr ="03145678"; qDebug() <<"String input ="<<myStr; // String input = "03145678" float myFloat = myStr.toFloat(); qDebug()<< "String to float conversion = " << myFloat; //= 3.14568e+06
I don't want the result as
3.14568e+06
, I want to display it as03145678
without losing any digit.When I tried it with
.toInt()
, I got the result as3145678
by losing the leading zero.How can I retain all the digits of the
String
while converting it to other data types likefloat
,double
orint
? -
Here's my code
QString myStr ="03145678"; qDebug() <<"String input ="<<myStr; // String input = "03145678" float myFloat = myStr.toFloat(); qDebug()<< "String to float conversion = " << myFloat; //= 3.14568e+06
I don't want the result as
3.14568e+06
, I want to display it as03145678
without losing any digit.When I tried it with
.toInt()
, I got the result as3145678
by losing the leading zero.How can I retain all the digits of the
String
while converting it to other data types likefloat
,double
orint
?wrote on 3 Mar 2022, 03:49 last edited by Pl45m4 3 Mar 2022, 04:06Same answer as here: Change the qDebug notation.
Haven't tried but there's something likeQt::fixed
andQt::scientific
which you can use in your print command.Edit:
Similar topic here:
-
qDebug() is for quickly checking your data.
If you want to format and display your data, convert it to a QString with the exact formatting that you want. Then, display it in a QLabel or another display widget.
-
qDebug() is for quickly checking your data.
If you want to format and display your data, convert it to a QString with the exact formatting that you want. Then, display it in a QLabel or another display widget.
wrote on 3 Mar 2022, 07:17 last edited by@JKSH said in How to get rid of scientific notation while converting from QString to float:
qDebug() is for quickly checking your data.
If you want to format and display your data, convert it to a QString with the exact formatting that you want. Then, display it in a QLabel or another display widget.
I'm running this code in Qt console.
-
@JKSH said in How to get rid of scientific notation while converting from QString to float:
qDebug() is for quickly checking your data.
If you want to format and display your data, convert it to a QString with the exact formatting that you want. Then, display it in a QLabel or another display widget.
I'm running this code in Qt console.
@Swati777999 said in How to get rid of scientific notation while converting from QString to float:
I'm running this code in Qt console.
OK, then use
QTextStream
to print to stdout.You cal call the following functions to format your numbers:
- QTextStream::setNumberFlags()
- QTextStream::setRealNumberNotation()
- QTextStream::setFieldWidth()
- QTextStream::setPadChar()
-
@Swati777999 said in How to get rid of scientific notation while converting from QString to float:
I'm running this code in Qt console.
OK, then use
QTextStream
to print to stdout.You cal call the following functions to format your numbers:
- QTextStream::setNumberFlags()
- QTextStream::setRealNumberNotation()
- QTextStream::setFieldWidth()
- QTextStream::setPadChar()
wrote on 3 Mar 2022, 09:34 last edited by@JKSH said in How to get rid of scientific notation while converting from QString to float:
@Swati777999 said in How to get rid of scientific notation while converting from QString to float:
I'm running this code in Qt console.
OK, then use
QTextStream
to print to stdout.You cal call the following functions to format your numbers:
- QTextStream::setNumberFlags()
- QTextStream::setRealNumberNotation()
- QTextStream::setFieldWidth()
- QTextStream::setPadChar()
It's so frustrating not to see the code working correctly when you follow Qt Docs.
QString myStr ="03145678"; qDebug() <<"String input ="<<myStr; qDebug()<<"After applying QTextStream = <<" << QTextStream::setRealNumberNotation(myStr::2); //Syntax Error
I am unable to understand the synatx from the Qt Doc. :( and it's so frustrating for me!
-
@JKSH said in How to get rid of scientific notation while converting from QString to float:
@Swati777999 said in How to get rid of scientific notation while converting from QString to float:
I'm running this code in Qt console.
OK, then use
QTextStream
to print to stdout.You cal call the following functions to format your numbers:
- QTextStream::setNumberFlags()
- QTextStream::setRealNumberNotation()
- QTextStream::setFieldWidth()
- QTextStream::setPadChar()
It's so frustrating not to see the code working correctly when you follow Qt Docs.
QString myStr ="03145678"; qDebug() <<"String input ="<<myStr; qDebug()<<"After applying QTextStream = <<" << QTextStream::setRealNumberNotation(myStr::2); //Syntax Error
I am unable to understand the synatx from the Qt Doc. :( and it's so frustrating for me!
@Swati777999 said in How to get rid of scientific notation while converting from QString to float:
setRealNumberNotation
You know that this is NOT a static method?
Also, why do you mix qDebug and QTextStream? This is NOT what @JKSH suggested.QTextStream out(stdout); out.setRealNumberNotation... out << ...
-
@JKSH said in How to get rid of scientific notation while converting from QString to float:
@Swati777999 said in How to get rid of scientific notation while converting from QString to float:
I'm running this code in Qt console.
OK, then use
QTextStream
to print to stdout.You cal call the following functions to format your numbers:
- QTextStream::setNumberFlags()
- QTextStream::setRealNumberNotation()
- QTextStream::setFieldWidth()
- QTextStream::setPadChar()
It's so frustrating not to see the code working correctly when you follow Qt Docs.
QString myStr ="03145678"; qDebug() <<"String input ="<<myStr; qDebug()<<"After applying QTextStream = <<" << QTextStream::setRealNumberNotation(myStr::2); //Syntax Error
I am unable to understand the synatx from the Qt Doc. :( and it's so frustrating for me!
wrote on 3 Mar 2022, 10:17 last edited by KroMignon 3 Mar 2022, 10:18@Swati777999 said in How to get rid of scientific notation while converting from QString to float:
It's so frustrating not to see the code working correctly when you follow Qt Docs.
QString myStr ="03145678"; qDebug() <<"String input ="<<myStr; qDebug()<<"After applying QTextStream = <<" << QTextStream::setRealNumberNotation(myStr::2); //Syntax Error
I am unable to understand the synatx from the Qt Doc. :( and it's so frustrating for me!
I don't want to hurt you, but this is standard C++ syntax. As I already told you C++ and Python are 2 different languages in almost all aspects.
So if you want to develop in C++ you have to learn C++ basics, as you have done for python.C++ is not better or worst than python, it is just completely different.
The C++ code you have written don't make sense, so it cannot work:
myStr::2
is non sense, this would mean you want to get a static member called2
(which is not allowed a member name!) from class used bymyStr
, which isQString
QTextStream::setRealNumberNotation()
means in C++ that you want to call static methodsetRealNumberNotation
of calssQTextStream
which do not exists!
So please, take time to learn C++ if you want to program in C++.
-
Here's my code
QString myStr ="03145678"; qDebug() <<"String input ="<<myStr; // String input = "03145678" float myFloat = myStr.toFloat(); qDebug()<< "String to float conversion = " << myFloat; //= 3.14568e+06
I don't want the result as
3.14568e+06
, I want to display it as03145678
without losing any digit.When I tried it with
.toInt()
, I got the result as3145678
by losing the leading zero.How can I retain all the digits of the
String
while converting it to other data types likefloat
,double
orint
?wrote on 4 Mar 2022, 07:56 last edited by@Swati777999 said in How to get rid of scientific notation while converting from QString to float:
When I tried it with .toInt() , I got the result as 3145678 by losing the leading zero.
There is no built-in number type that would store leading zeros. If this is what you want you need to look for another solution. You can specify the width of the output with a padding of '0's to the left. What kind of numbers do you want to handle? What do they represent? Why should they have a leading zero?
1/9