Details
-
Bug
-
Resolution: Done
-
P3: Somewhat important
-
5.1.1
-
None
-
9c8943a862a5762eef0ae98f2b6b14e5c449105c
Description
The documentation for QString.toDouble(bool * ok) indicates that it:
Returns the string converted to a double value;
Returns 0.0 if the conversion fails.
If a conversion error occurs, *ok is set to false;
otherwise *ok is set to true.
Various string formats for floating point numbers can be converted to double values
This is fine as far as it goes. The problem is what is NOT in the documentation – Specifically what conditions cause an error. In point of fact, the user has to write a small program and test for failure modes (For example, a program with a single QLineEdit whose text is analyzed after any change and which reports the state of “ok” and the value of the converted number). Then, after trying every possible combination of numbers and text one can think of, construct a set of rules.
It turns out that “toDouble(bool* ok) will fail if there are any extraneous characters in the QString. The number may contain only an optional sign, digits, a decimal point if needed, an optional exponent specification for scientific notation, which can be either g, G, e or E, the exponent sign and an integer exponent. The presence of virtually any other characters (except spaces) will cause failure. For example, attempting to include a UNIT with a number (as in 100 V) will fail.
Incidentally, none of the numeric conversion functions actually indicate what will produce an error. Even the addition of a simple phrase to the ducumentation such as: “the QString may contain ONLY a valid number. No additional characters are permitted at all.” would save the programmer several hours of testing, searching and reading.
Attachments
Issue Links
- relates to
-
QTBUG-66115 Let (QString|QLocale)::toFloat ignore trailing non-digits
- Open