Question about CString Format
-
Hi, so here's my problem: Basically I want to format a CString such that the trailing zeroes are all eliminated. For example, let's say I have a double such as: 610.15100000 And I want to print this to a CString so that I can set the text in an edit control. Right now I do something like this:
CString strTemp; double fVal = 610.151; strTemp.Format(TEXT("%lf%), fVal);
But in the edit control, it shows up with all the zeroes at the end: 610.15100000000 How do I make it print so that it shaves off the extra zeroes at the end? Thanks!KR
-
Hi, so here's my problem: Basically I want to format a CString such that the trailing zeroes are all eliminated. For example, let's say I have a double such as: 610.15100000 And I want to print this to a CString so that I can set the text in an edit control. Right now I do something like this:
CString strTemp; double fVal = 610.151; strTemp.Format(TEXT("%lf%), fVal);
But in the edit control, it shows up with all the zeroes at the end: 610.15100000000 How do I make it print so that it shaves off the extra zeroes at the end? Thanks!KR
KellyR wrote:
How do I make it print so that it shaves off the extra zeroes at the end?
Assuming you want three places after the decimal, use:
strTemp.Format(TEXT("%.3f"), fVal);
If, however, you are wanting to remove all trailing zeros regardless of how many, see the
TrimRight()
method."Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
modified on Friday, April 18, 2008 11:58 AM
-
Hi, so here's my problem: Basically I want to format a CString such that the trailing zeroes are all eliminated. For example, let's say I have a double such as: 610.15100000 And I want to print this to a CString so that I can set the text in an edit control. Right now I do something like this:
CString strTemp; double fVal = 610.151; strTemp.Format(TEXT("%lf%), fVal);
But in the edit control, it shows up with all the zeroes at the end: 610.15100000000 How do I make it print so that it shaves off the extra zeroes at the end? Thanks!KR
You may use
strTmp.TrimRight(_T("0"));
to the purpose. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke -
KellyR wrote:
How do I make it print so that it shaves off the extra zeroes at the end?
Assuming you want three places after the decimal, use:
strTemp.Format(TEXT("%.3f"), fVal);
If, however, you are wanting to remove all trailing zeros regardless of how many, see the
TrimRight()
method."Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
modified on Friday, April 18, 2008 11:58 AM
The above will fail on
fVal = 1.051000
(provided I understood his requirements). [added] Actually it fails also on his input (610.15100000
). [/added] :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke -
The above will fail on
fVal = 1.051000
(provided I understood his requirements). [added] Actually it fails also on his input (610.15100000
). [/added] :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain ClarkeCPallini wrote:
The above will fail on fVal = 1.051000
How so?
strTemp
has a value of 1.CPallini wrote:
Actually it fails also on his input (610.15100000).
It worked fine for me.
strTemp
has a value of 610."Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
You may use
strTmp.TrimRight(_T("0"));
to the purpose. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke -
CPallini wrote:
The above will fail on fVal = 1.051000
How so?
strTemp
has a value of 1.CPallini wrote:
Actually it fails also on his input (610.15100000).
It worked fine for me.
strTemp
has a value of 610."Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
DavidCrow wrote:
It worked fine for me. strTemp has a value of 610.
But it needs to be 610.151. Thanks though!
KR
So use
.3
instead of.0
then. :rolleyes:"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
CPallini wrote:
The above will fail on fVal = 1.051000
How so?
strTemp
has a value of 1.CPallini wrote:
Actually it fails also on his input (610.15100000).
It worked fine for me.
strTemp
has a value of 610."Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
I think he wants to remove trailing zeroes in a general way, i.e. that his input was just an example. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke -
I think he wants to remove trailing zeroes in a general way, i.e. that his input was just an example. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain ClarkeCPallini wrote:
I think he wants to remove trailing zeroes...
Yeah, I read it wrong. I thought he wanted to remove everything after the decimal. Sorry for the confusion.
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne