[Qt-interest] Cutting useless decimal places
Karl Krach
mailinglists at blueSpirit.la
Mon Oct 25 21:26:16 CEST 2010
Hello Atlant,
I don't know if my translation into English is correct, but in physics
you differ between
* valid digits and
* decimal places.
You have implemented '4 valid digits'. This would be ok for me, but
better would be if Qt would provide me some function which just removes
all 'unneeded zeros' on the right.
0.000125346 -> 0.000125346
0.001253457 -> 0.001253457
0.012534568 -> 0.012534568
0.125345679 -> 0.125345679
1.253456789 -> 1.253456789
12.534567890 -> 12.53456789
125.345678900 -> 125.3456789
1253.456789000 -> 1253.456789
12534.567890000 -> 12534.56789
12534.567800000 -> 12534.5678
12534.567000000 -> 12534.567
12534.560000000 -> 12534.56
12534.500000000 -> 12534.5
12534.510000000 -> 12534.51
12534.000000000 -> 12534
but also
5.000000000 -> 5
5.000100000 -> 5.0001
Maybe I will create something like this:
QString float2string(float number)
{
return QString("%1").arg( number, 0, 'f' )
.remove( QRegExp("\\.?0*$") );
}
original: "200.000000"
new: "200"
original: "20.000000"
new: "20"
original: "0.500000"
new: "0.5"
original: "0.125000"
new: "0.125"
original: "7000.000000"
new: "7000"
Thanks,
Charly
On 10/25/2010 01:56 PM, Atlant Schmidt wrote:
> Charly:
>
> Here's something to get you started / help us all
> think about your question.
>
> This produces the following output:
>
> 0.000125346 -> 0.000
> 0.001253457 -> 0.001
> 0.012534568 -> 0.013
> 0.125345679 -> 0.125
> 1.253456789 -> 1.253
> 12.534567890 -> 12.54
> 125.345678900 -> 125.3
> 1253.456789000 -> 1253
> 12534.567890000 -> 12535
> 12534.567800000 -> 12535
> 12534.567000000 -> 12535
> 12534.560000000 -> 12535
> 12534.500000000 -> 12534
> 12534.510000000 -> 12535
> 12534.000000000 -> 12534
> 602214178999999989284864.000000000 -> 602214178999999989284864
>
> Atlant
>
>
> #include<stdio.h>
> #include<math.h>
>
>
> // Forward routines
> //
>
> int main(int argc, char **argv);
> void try_one( double input );
>
>
> int main(int argc, char **argv)
> {
>
> try_one( 0.0001253456789 );
> try_one( 0.001253456789 );
> try_one( 0.01253456789 );
> try_one( 0.1253456789 );
> try_one( 1.253456789 );
> try_one( 12.53456789 );
> try_one( 125.3456789 );
> try_one( 1253.456789 );
> try_one( 12534.56789 );
> try_one( 12534.5678 );
> try_one( 12534.567 );
> try_one( 12534.56 );
> try_one( 12534.5 );
> try_one( 12534.51 );
> try_one( 12534.0 );
> try_one( 6.02214179E23 );
>
> return 0;
>
> }
>
>
> void try_one( double input )
> {
>
> double output;
> double absval;
>
> output = round(input*1000.0)/1000.0;
> absval = fabs( output );
>
> if (absval< 10.0)
> printf( "%.9f -> %.3f\n", input, output );
> else if (absval< 100.0)
> printf( "%.9f -> %.2f\n", input, output );
> else if (absval<1000.0)
> printf( "%.9f -> %.1f\n", input, output );
> else
> printf( "%.9f -> %.0f\n", input, output );
>
> }
>
>
>
> -----Original Message-----
> From: qt-interest-bounces at trolltech.com [mailto:qt-interest-bounces at trolltech.com] On Behalf Of mailinglists at bluespirit.la
> Sent: Monday, October 25, 2010 04:04
> To: qt-interest at trolltech.com
> Subject: [Qt-interest] Cutting useless decimal places
>
> 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,
>
> Charly
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
> This e-mail and the information, including any attachments, it contains are intended to be a confidential communication only to the person or entity to whom it is addressed and may contain information that is privileged. If the reader of this message is not the intended recipient, you are hereby notified that any dissemination, distribution or copying of this communication is strictly prohibited. If you have received this communication in error, please immediately notify the sender and destroy the original message.
>
> Thank you.
>
> Please consider the environment before printing this email.
>
More information about the Qt-interest-old
mailing list