CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Guest

    another simple question

    I have this code:

    float myFloat = 100.520920
    CString myString;
    myString.Format("%f",myFloat);

    I want the string to display 100.52 (limit to two decimal places)
    how can I do this?


  2. #2
    Join Date
    Apr 1999
    Location
    Germany
    Posts
    418

    Re: another simple question

    Hi,

    myString.Format("%.2f",myFloat);

    The number after the dot is the count of the decimal places. Format ist calling sprintf, it will be rounded. 0.4999 will be 0, 0.5 will be 1.

    BTW, if you're writing a "real" application (not just for learning)you need to take care of the regional settings. Some countries use a comma as decimal point.

    Martin

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