PDA

View Full Version : String ( QbyteArray ) to Double Value with 6 decimal digits



persianpatient
30th August 2015, 11:43
Hi ,

I have to convert a string to a double value , my string is inside a QByteArray :


QByteArray myStr = "50.123456";
double myDouble = myStr.toDouble();

it will return 50.12346 , I need exact value , how to fix this problem ?

Best Regards,
Amir .

anda_skoa
30th August 2015, 11:58
This could be the exact value, but our output doesn't have enough precision, or it is the closest value a double can hold.

Cheers,
_

ChrisW67
30th August 2015, 22:43
The closest approximation of 50.123456 in a double is 5.01234559999999973456397128757E1 (5.0123455047607421875E1 in a float). If you must have the exact value then you should not be using double but some precise storage mechanism, an integer number of millionths for example, and do any manipulations in that form.

If all you need is to be able to reproduce the string representation to six decimal places then either float or double approximation may be acceptable but you need to format the number correctly with QString::number() or the like.