|
-
November 24th, 2003, 03:35 PM
#1
CString format question...
I'm trying to get a float formatted to a CString and displayed in an Edit box. So I'm going to do it like
PHP Code:
m_sStrRange.Format("%3.2f", m_fRange);
but I don't want to show trailing zeros. That is if the number is "19.00" I want to show "19", NOT "19.00". According to MSDN I have to use the # flag in my format statement and that's my problem. How do I use it in conjunction with the other flags in my code example?
Last edited by Thresher; November 24th, 2003 at 03:39 PM.
-
November 24th, 2003, 04:04 PM
#2
I believe the following should work:
m_sStrRange.Format("%#3.2g", m_fRange);
The # only truncates in the case that you are using the g or G as type instead of f or F. g is the type for double, f is the type for float.
-
November 24th, 2003, 06:40 PM
#3
Actually, "%3.2g" will work fine. The # flag is used only in conjunction with octal or hex format specifiers.
-
November 25th, 2003, 04:52 PM
#4
Originally posted by gstercken
Actually, "%3.2g" will work fine. The # flag is used only in conjunction with octal or hex format specifiers.
Thanks for the help... but it's still not working. It does removed the trailing zeros (2.60 becomes 2.6) but now this code...
PHP Code:
m_sMaxRange.Format("%3.2g",(Cd->getMaxRange()));
... when I debug it has Cd->getMaxRange returning 1.99 and then the string m_sMaxRange displays a 2 ! Not a 2.00, just a 2! And when I made the value a 2.57 it displayed a 2.6?! I don't understand....
-
November 26th, 2003, 09:11 AM
#5
BTT... anyone have any ideas?
-
November 26th, 2003, 09:42 AM
#6
From MSDN on the printf format specifications:
A format specification, which consists of optional and required fields, has the following form:
%[flags] [width] [.precision] [{h | l | I | I32 | I64}]type
Try changing it to "%.3g". The "3" here is the precision so if you say 2 then you are only going to get 2 digits. ie: X.X If you say three you can get the following: XX.X or X.XX or XXX. You need to change this value to the max number of characters you will need to display.
-
November 26th, 2003, 10:06 AM
#7
Originally posted by Adam Gritt
Try changing it to "%.3g". The "3" here is the precision so if you say 2 then you are only going to get 2 digits. ie: X.X If you say three you can get the following: XX.X or X.XX or XXX. You need to change this value to the max number of characters you will need to display.
Thanks Adam... I'll give it a try...
-
November 26th, 2003, 02:54 PM
#8
Worked like a charm! Thanks Adam!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|