PDA

View Full Version : int to QString with points?



realdarkman71
19th May 2011, 11:11
Hi all,

how can I convert a int to QString with points between?


// qint64 12282825169 = size of a directory
QString str = QString::number(12282825169) + " Bytes"; // str = "12282825169 Bytes"
but I need:


str = "12.282.825.169 Bytes"
How can I do this?

Thx!
Chris

meazza
19th May 2011, 11:20
Well first convert to normal string and then you can use the insert function to insert points where you like and append "Bytes" at the end

realdarkman71
19th May 2011, 11:59
How? The string/qint64 is differently long!

DanH
19th May 2011, 12:16
It requires programming. Create the string view of the number, break it apart (with mid()), insert the dots (append()).

[Oops -- forgot about insert() -- probably the better approach. Remember to work from the tail end.]

meazza
19th May 2011, 12:27
How? The string/qint64 is differently long!

Well you have to figure the logic your self. But something like you check how long it is and then depending on that you put the dots where you want them.

Lesiok
19th May 2011, 14:25
QLOcale::toString produces a localized string representation of the given number.

realdarkman71
19th May 2011, 18:44
QLOcale::toString produces a localized string representation of the given number.

This is exactly what I looked for! Thanks for your help!

ChrisW67
19th May 2011, 23:19
Your "points" are thousand or group separators. If you want the number formatted with thousands separators appropriate for the user's locale then the QLocale methods are the ones to use. Depending on where in the world you are you will get different characters in this role, most commonly a comma.

If you actually wanted to insert periods "." into the string regardless of your locale settings, then you will need to do this yourself or force QLocale to always use a locale with "." as the group separator.