You can format a number as a string with a specified number of decimal places using QString::number() and the 'f' format (or its QLocale equivalent). If you need to present exactly what the user entered back to the user then keep it as a string. Once you convert it to float you have no way to distinguish between "100", "100.0" and "100.00000": 100 is 100 (and integer) no matter how many zeros you append after the decimal point.
Recognising a string as a floating point or integer number is not trivial unless you restrict yourself to one location. For example, "1,100.0" is a number in some locales and not others where it may be expressed as "1.000,0" or "1000.0". You could use a simple regular expression to tell "100.0" apart from "100" but you have to know exactly what your input might look like. What would you do with 1100 expressed as "1.1e3" or "100." for example?
Bookmarks