我希望在Qt中显示一个带有固定数字位数(4)的浮点数,但不填充未使用的数字为零(相当于具有最大位数)。换句话说,这就是我想要为以下示例展示的内容:
等。我尝试了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]));
}
答案 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();
}