Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development


Thank you for visiting the BlackBerry Support Community Forums.

BlackBerry will be closing the BlackBerry Support Community Forums Device Forums on April 1st (Developers, see below)

BlackBerry remains committed to providing excellent customer support to our customers. We are delighted to direct you to the CrackBerry Forums, a well-established and thorough support channel, for continued BlackBerry support. Please visit http://forums.crackberry.com or http://crackberry.com/ask. You can also continue to visit BlackBerry Support or the BlackBerry Knowledge Base for official support options available for your BlackBerry Smartphone.

"When we launched CrackBerry.com 10 years ago, we set out to make it a fun and useful destination where BlackBerry Smartphone owners could share their excitement and learn to unleash the full potential of their BlackBerry. A decade later, the CrackBerry community is as active and passionate as ever and I know our knowledgeable members and volunteers will be excited to welcome and assist more BlackBerry owners with their questions."

- Kevin Michaluk, Founder, CrackBerry.com

Developers, for more information about the BlackBerry Developer Community please review Join the Conversation on the BlackBerry Developer Community Forums found on Inside BlackBerry.


Reply
Developer
Posts: 194
Registered: ‎11-24-2012
My Device: Dev Alpha B, Bold 9700
My Carrier: NA

format QString for float?

I've been looking through the docs here:

http://qt-project.org/doc/qt-5.0/qtcore/qstring.html#arg

 

But can't seem to find a way to format a float/qreal value:

qreal floatValue = 1.1;
QString valueString = QString("%1").arg(floatValue); // should produce "1.10" with 2 decimal places following

 How can this be done?

Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: format QString for float?

If you need a quick solution for default C locale, here's a function I used for converting long doubles to QString. Decimal separator is a dot. Comment out "trim trailing zeros" section.

 

std::ostringstream o;
o.precision(2);
o << std::fixed;
o << value;
std::string s = o.str();

// Trim trailing zeros
size_t decimalPos = s.find_last_of('.');
if (decimalPos != std::string::npos)
{
  std::string::size_type trimAfter = s.find_last_not_of('0');
  if (trimAfter != std::string::npos)
    s.erase(trimAfter + 1);
  else
    s.clear(); // should never happen

  // Remove decimal dot at the end:
  decimalPos = s.find_last_of('.');
  if (decimalPos == s.length() - 1)
    s.erase(decimalPos, 1);
}

QString valueAsQString = QString::fromStdString(s);

 


Andrey Fidrya, @zmeyc on twitter
Highlighted
Developer
Posts: 194
Registered: ‎11-24-2012
My Device: Dev Alpha B, Bold 9700
My Carrier: NA

Re: format QString for float?

I think your way is more compatible with porting, but if anyone is using QString quite a bit, then this page has some good formatting info:

http://www.qtforum.org/article/29565/output-a-double-to-only-2-decimal-points.html

 

Thanks Zmey