[Qt-interest] Cutting useless decimal places
Atlant Schmidt
aschmidt at dekaresearch.com
Mon Oct 25 21:35:46 CEST 2010
Karl:
> ...better would be if Qt would provide me some function
> which just removes all 'unneeded zeros' on the right.
In that case, I'd probably just sprintf() into a buffer
and, looking at the result buffer from the right-most
character towards the left, truncate all of the zeroes.
This is one of those cases where it's trivially easy to
implement a crude-but-100%-effective solution whereas
you could spend a long time looking for an "elegant"
solution (such as some already-defined method that
does just what you want) ;-).
Atlant
-----Original Message-----
From: Karl Krach [mailto:mailinglists at blueSpirit.la]
Sent: Monday, October 25, 2010 15:26
To: Atlant Schmidt
Cc: qt-interest at trolltech.com
Subject: Re: [Qt-interest] Cutting useless decimal places
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
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