[Qt-interest] Cutting useless decimal places

Karl Krach mailinglists at blueSpirit.la
Mon Oct 25 21:24:10 CEST 2010


Hello Andre,

I have to print 2^x : "1024" as well as "0.125" or "0.25". When 
displaying calculated results I will take the problems with float into 
account, thx.

I just want to have no zeros at the end. I think I will do it with 
something like this:

QString float2string(float number)
{
	return QString("%1").arg( number, 0, 'f' )
                             .remove( QRegExp("\\.?0*$") );
}

OK, it must be optimized - but it is localized and prints what I want. I 
just thought, that Qt has a function with is easier to use :)

Best regards,

Charly

On 10/25/2010 12:09 PM, Andre Somers wrote:
> Hi,
>
> Op Ma, 25 oktober, 2010 10:03 am, schreef mailinglists at bluespirit.la:
>> Hello,
>>
>> a very simple question: How can I create a QString from a
>> float/double, in format 'f' without giving a explicit number of decimal
>> places to the arg() arguments?
>>
>> qDebug()<<  QString("Hello world: %1").arg( fNumber, 0, 'f' );
>>
>> When setting fNumbers to '0.125' I want an output of '0.125'. But when
>> setting the fNumbers variable to '200', I don't want an output of
>> "200.000". I want also use the localized output - so I prefer
>> QString::arg() to QString::sprintf(). The use of precision==-1 is not
>> documented - at least not in qt 4.6 api-doc.
>>
>> Thank in advance,
>
> What kind of output are you looking for *exactly*? Do you want to get
> output to the a pre-defined number of significant digits? What should be
> the output if your number is 2001? Or 200001? And 0.00200001? Did you take
> into account that floating point numbers are often not precise?
>
> I once wrote a function that returns "2.00 thousand" and "200 thousand"
> for the first two cases (it does not currently support numbers<  1) so you
> get some reasonable precision that is fit for human consumption. But I am
> not sure that is what you are after.
>
> André
>
>
>
>



More information about the Qt-interest-old mailing list