CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    85

    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.

  2. #2
    Join Date
    Nov 2003
    Posts
    74
    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.

  3. #3
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815
    Actually, "%3.2g" will work fine. The # flag is used only in conjunction with octal or hex format specifiers.

  4. #4
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    85
    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....

  5. #5
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    85
    BTT... anyone have any ideas?

  6. #6
    Join Date
    Nov 2003
    Posts
    74
    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.

  7. #7
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    85
    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...

  8. #8
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    85
    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
  •  





Click Here to Expand Forum to Full Width

Featured