如何在Qt中显示具有固定位数的浮动数字?

时间:2015-09-11 13:53:55

标签: qt

我希望在Qt中显示一个带有固定数字位数(4)的浮点数,但不填充未使用的数字为零(相当于具有最大位数)。换句话说,这就是我想要为以下示例展示的内容:

  • 0 - > 0.0
  • 10 - > 10.0
  • 980.5 - > 980.5
  • 1200.5 - > 1.200 k
  • 9900.9 - > 9.900 k
  • 120500.9 - > 120.5 k
  • 999888.88 - > 999.9 k

等。我尝试了QString::number()QString::args()的许多组合,但没有成功。那我怎么能这样做呢?

注意:我知道对于高于1000的数字,我必须应用一个部门并添加标签' k'手动 - 我已经这样做了。

修改 以下代码正是我想要的,只是它对所有if else都是不合适的。我想知道如何使用Qt的功能:

float temp = getSomeValue();
const char* itemUnities[] = { "V", "W", "A", "J" };

if (temp < 10.0f)
{
    painter.drawText(defaultX + 60,yPosition,QString::number(temp,'f',3));
    painter.drawText(defaultX + 110,yPosition,tr(itemUnities[aaa]));
}
else if (temp < 100.0f)
{
    painter.drawText(defaultX + 60,yPosition,QString::number(temp,'f',2));
    painter.drawText(defaultX + 110,yPosition,tr(itemUnities[aaa]));
}
else if (temp < 1000.0f)
{
    painter.drawText(defaultX + 60,yPosition,QString::number(temp,'f',1));
    painter.drawText(defaultX + 110,yPosition,tr(itemUnities[aaa]));
}
else if (temp < 10000.0f)
{
    temp *= 0.001;

    painter.drawText(defaultX + 60,yPosition,QString::number(temp,'f',3));
    painter.drawText(defaultX + 110,yPosition,tr("k") + tr(itemUnities[aaa]));
}
else if (temp < 100000.0f)
{
    temp *= 0.001;

    painter.drawText(defaultX + 60,yPosition,QString::number(temp,'f',2));
    painter.drawText(defaultX + 110,yPosition,tr("k") + tr(itemUnities[aaa]));
}
else if (temp < 1000000.0f)
{
    temp *= 0.001;

    painter.drawText(defaultX + 60,yPosition,QString::number(temp,'f',1));
    painter.drawText(defaultX + 110,yPosition,tr("k") + tr(itemUnities[aaa]));
}
else if (temp < 10000000.0f)
{
    temp *= 0.000001;

    painter.drawText(defaultX + 60,yPosition,QString::number(temp,'f',3));
    painter.drawText(defaultX + 110,yPosition,tr("M") + tr(itemUnities[aaa]));
}
else if (temp < 100000000.0f)
{
    temp *= 0.000001;

    painter.drawText(defaultX + 60,yPosition,QString::number(temp,'f',2));
    painter.drawText(defaultX + 110,yPosition,tr("M") + tr(itemUnities[aaa]));
}
else if (temp < 1000000000.0f)
{
    temp *= 0.000001;

    painter.drawText(defaultX + 60,yPosition,QString::number(temp,'f',1));
    painter.drawText(defaultX + 110,yPosition,tr("M") + tr(itemUnities[aaa]));
}

1 个答案:

答案 0 :(得分:2)

由于您对转换有非常具体的要求,因此内置方法不提供所有功能,但通过一些技巧,您可以使用QString::number()QString::truncate()

QString doubleToQStr(const double val, const size_t d)
{
  QString str = QString::number(val,'g',15);

  if( val >= std::pow(10.0,static_cast<double>(d-1)) ) 
  { 
    str.truncate(d); 

    // find magnitude
    size_t mag = 0;
    while( val >= std::pow( 10.0, static_cast<double>(mag) ) ) { mag++; } 

    if ( mag > 3 )
    {
      size_t dotpos = mag % 3;
      str.insert(dotpos,".");

      size_t mag3 = mag - dotpos;

      switch( mag3 )
      { 
      case 3:
        str += " k"; break;
      case 6:
        str += " M"; break;
      case 9:
        str += " G"; break;
      case 12:
        str += " T"; break;
      default:
        str += " ?"; break;
      }
    }
  }
  else if ( val < std::pow(10.0,static_cast<double>(-(static_cast<int>(d)-1))) )
  { str = "0.0"; }
  else
  { str.truncate(d+1); }  

  return str;
}

以下是我使用的测试用例:

int main(int argc, char *argv[])
{
  QApplication a(argc, argv);

  std::cout << "99919999.9 -> " << doubleToQStr(99919999.9, 4).toStdString() << std::endl;
  std::cout << "9999.9 -> " << doubleToQStr(9999.9, 4).toStdString() << std::endl;
  std::cout << "999.9 -> " << doubleToQStr(999.9, 4).toStdString() << std::endl;
  std::cout << "99.9 -> " << doubleToQStr(99.9, 4).toStdString() << std::endl;
  std::cout << "9.9 -> " << doubleToQStr(9.9, 4).toStdString() << std::endl;
  std::cout << "0.9 -> " << doubleToQStr(0.9, 4).toStdString() << std::endl;
  std::cout << "0.09 -> " << doubleToQStr(0.09, 4).toStdString() << std::endl;
  std::cout << "0.009 -> " << doubleToQStr(0.009, 4).toStdString() << std::endl;
  std::cout << "0.0009 -> " << doubleToQStr(0.0009, 4).toStdString() << std::endl;
  std::cout << "0.00009 -> " << doubleToQStr(0.00009, 4).toStdString() << std::endl;

    return a.exec();
}