Loss of '0's while converting from QString to float or QString to double
-
I wrote the following codes for converting QString to double and QString to float
EXAMPLE-1
: Conversion from QString to doubleQString myStr ="3.14000"; double myStr_double = myStr.toDouble(); qDebug() << "String to double conversion =" <<myStr_double; // Ans :3.14
EXAMPLE-2
: Conversion from QString to floatQString myStr ="3.14000"; float myStr_float = myStr.toFloat(); qDebug() << "String to float conversion =" <<myStr_float; // Ans :3.14
In both the above cases, I'm getting
3.14
whereas I want to get the answer as3.14000
without losing zeroes. Can someone suggest to me how to do it? -
There's always a lot of confusions about string representations of floating point numbers.
You have to think of the internal value of a float number as undeterminate until you decide to print it on the screen.
And you have to decide the format you want to see ( number of digits, precision)asprintf is very handy for that:
float f=3.14; qDebug()<<QString::asprintf("%10f",f); // precision=6 by default qDebug()<<QString::asprintf("%10.4f",f); qDebug()<<QString::asprintf("%10.2f",f); qDebug()<<QString::asprintf("%010.2f",f);
" 3.140000" " 3.1400" " 3.14" "0000003.14"
-
I wrote the following codes for converting QString to double and QString to float
EXAMPLE-1
: Conversion from QString to doubleQString myStr ="3.14000"; double myStr_double = myStr.toDouble(); qDebug() << "String to double conversion =" <<myStr_double; // Ans :3.14
EXAMPLE-2
: Conversion from QString to floatQString myStr ="3.14000"; float myStr_float = myStr.toFloat(); qDebug() << "String to float conversion =" <<myStr_float; // Ans :3.14
In both the above cases, I'm getting
3.14
whereas I want to get the answer as3.14000
without losing zeroes. Can someone suggest to me how to do it?You dont "lose" them... it's just the
qDebug
output which prints it like that.See:
and further:
-
@Swati777999
You seem to be confusing the human-readable representation of the number (i.e. your myStr) with the value of the number (i.e. myStr_double).3.14 is the numeric value. How that is converted into a human-readable representation is entirely up to you. Suppressing leading and trailing zeroes (because they generally carry no meaning) is the default behaviour of the vast majority of conversions from numeric value to readable-string ; in this case qDebug. Aside from @Pl45m4's suggestions you should also look at QString::number() and use a suitable precision (e.g. 5).
-
There's always a lot of confusions about string representations of floating point numbers.
You have to think of the internal value of a float number as undeterminate until you decide to print it on the screen.
And you have to decide the format you want to see ( number of digits, precision)asprintf is very handy for that:
float f=3.14; qDebug()<<QString::asprintf("%10f",f); // precision=6 by default qDebug()<<QString::asprintf("%10.4f",f); qDebug()<<QString::asprintf("%10.2f",f); qDebug()<<QString::asprintf("%010.2f",f);
" 3.140000" " 3.1400" " 3.14" "0000003.14"