[Qt-interest] [BUG?]QLocale::to* functions and trailing whitespace

Denis Dzyubenko denis.dzyubenko at trolltech.com
Fri Mar 27 16:46:58 CET 2009


Hi Mihail,

Mihail Naydenov wrote:
> The documentation states that all QLocale::to* functions (these are QLocale::toShort, QLocale::toLongLong, etc)
> "ignores leading and trailing whitespace."
> 
> It turned out however that 'C' locale ( QLocale cl(QLocale::C); ) does leave trailing withespace!
> It handles all other - leading, and in-between spaces, but leaves trialing. 
> Using the default local on my machine works fine and leaves no witespace.

What do you mean by "leaving a whitespace"? The listed functions are not 
supposed to modify the provided string.

QLocale locale(QLocale::C);
int value = locale.toInt("   1234   "); // value equals 1234

> Also using QString::to* functions acts as local 'C' leaving only tailing.
> 
> I have two questions:
> 
> Is this intended or a bug?
> 
> What is The Right Way to convert strings to numbers - using string::to* or with local()->to*?
> Looking in the code, String calls QLocale in the end, so is there a difference? 

Both ways are correct. If you want to convert data using the current 
system locale, you can just use conversion functions from QString. 
However if you want to convert double to a string, save it somewhere and 
do the string to double conversion later on another machine on another 
locale, then you might want to explicitely say which locale should be 
used for the conversion.

-- 
Denis Dzyubenko
Software Engineer
Nokia, Qt Software



More information about the Qt-interest-old mailing list