CString Format method
-
Hi, I'm trying to control the number of values displayed after the decimal point in a CString. I have a variable which holds the number of values I wish to see after the decimal point. Is there any way of using this variable in the Format method? So rather than having: x.Format(_T("%.5f"), number) I would like the 5 ( or whatever number ) to be replaced by my variable. Thanks.
-
Hi, I'm trying to control the number of values displayed after the decimal point in a CString. I have a variable which holds the number of values I wish to see after the decimal point. Is there any way of using this variable in the Format method? So rather than having: x.Format(_T("%.5f"), number) I would like the 5 ( or whatever number ) to be replaced by my variable. Thanks.
CString s; s.Format("%%.%df", 5") s.Format(s, .2346324523); The idea is that the double %% gets turned into a single one, and the single one gets used to put your value in. Christian We're just observing the seasonal migration from VB to VC. Most of these birds will be killed by predators or will die of hunger. Only the best will survive - Tomasz Sowinski 29-07-2002 ( on the number of newbie posters in the VC forum ) Cats, and most other animals apart from mad cows can write fully functional vb code. - Simon Walton - 6-Aug-2002
-
CString s; s.Format("%%.%df", 5") s.Format(s, .2346324523); The idea is that the double %% gets turned into a single one, and the single one gets used to put your value in. Christian We're just observing the seasonal migration from VB to VC. Most of these birds will be killed by predators or will die of hunger. Only the best will survive - Tomasz Sowinski 29-07-2002 ( on the number of newbie posters in the VC forum ) Cats, and most other animals apart from mad cows can write fully functional vb code. - Simon Walton - 6-Aug-2002
I keep getting a run-time error. My code is: strx.Format(_T("%%.%f"), signif); strx.Format(strx, m_nMolarity); where signif is the variable that I want to use to specify the number of decimal places after the point and m_nMolarity is the variable which requires formatting. Have I misinterpreted your code? Thanks
-
I keep getting a run-time error. My code is: strx.Format(_T("%%.%f"), signif); strx.Format(strx, m_nMolarity); where signif is the variable that I want to use to specify the number of decimal places after the point and m_nMolarity is the variable which requires formatting. Have I misinterpreted your code? Thanks
You lost the d. "%%.%df", signif formats to %.5f if signif = 5; then format that with m_nMolarity. Christian We're just observing the seasonal migration from VB to VC. Most of these birds will be killed by predators or will die of hunger. Only the best will survive - Tomasz Sowinski 29-07-2002 ( on the number of newbie posters in the VC forum ) Cats, and most other animals apart from mad cows can write fully functional vb code. - Simon Walton - 6-Aug-2002
-
I keep getting a run-time error. My code is: strx.Format(_T("%%.%f"), signif); strx.Format(strx, m_nMolarity); where signif is the variable that I want to use to specify the number of decimal places after the point and m_nMolarity is the variable which requires formatting. Have I misinterpreted your code? Thanks
Yo've missed one little thing: You have:
"%%.%f"
, and You should have"%%.%df"
And of course signif must be an integer value;"%%.%f"
tries to interpret your signif as floating poin value and it doez not produce the f char for m_nMorality."%%.%df"
will produce"%.f"
[ CoY0te ] Railgun is like a Gilette Mach 3 - it does the job with one, easy stroke. -
You lost the d. "%%.%df", signif formats to %.5f if signif = 5; then format that with m_nMolarity. Christian We're just observing the seasonal migration from VB to VC. Most of these birds will be killed by predators or will die of hunger. Only the best will survive - Tomasz Sowinski 29-07-2002 ( on the number of newbie posters in the VC forum ) Cats, and most other animals apart from mad cows can write fully functional vb code. - Simon Walton - 6-Aug-2002
What a cunning stunt! I'm very impressed. STL is a religeon. Enquiries to Reverend Christian Graus
-
Hi, I'm trying to control the number of values displayed after the decimal point in a CString. I have a variable which holds the number of values I wish to see after the decimal point. Is there any way of using this variable in the Format method? So rather than having: x.Format(_T("%.5f"), number) I would like the 5 ( or whatever number ) to be replaced by my variable. Thanks.
Hi, The easiest way to do this is to use the '*' modifier in your format string. This allows you to specify the number of digits with a variable. The following will format using a variable number of digits: int precision = 5; x.Format( _T("%.*f"), precision, number ); You can also use a * for the width of the field, so the following will set the width and precision: int width = 10; int precision = 3; x.Format( _T("%*.*f"), width, precision, number ); Best regards, John