float in the string

Discussion in 'MFC' started by inspiration, May 30, 2010.

  1. inspiration

    inspiration New Member

    Joined:
    Feb 15, 2010
    Messages:
    85
    Likes Received:
    0
    Trophy Points:
    0
    Hi,
    how can I in a floatvariable a String "installation when" the float to 2 digits after the decimal point limit?
     
  2. pankaj.sea

    pankaj.sea New Member

    Joined:
    Apr 6, 2009
    Messages:
    461
    Likes Received:
    13
    Trophy Points:
    0
    Occupation:
    Web Developer
    Location:
    Kolkata
    Home Page:
    http://ipankaj.net
    One can for example, the first floating point number two decimal places to round out the string and then write.
    Here's an example code for rounding:

    Code:
    #include <iostream> 
    using namespace std; 
    
    int main() 
    { 
        float zahl; 
        cin >> zahl; 
        zahl = int( 100*zahl + 0.5 ) / 100.0; //runden auf 2 Nachkommastellen 
        cout << zahl; 
        return 0; 
    }
     
  3. inspiration

    inspiration New Member

    Joined:
    Feb 15, 2010
    Messages:
    85
    Likes Received:
    0
    Trophy Points:
    0
    Thank you

    forgot to say that I Basisstation a CString.
    The rounds went well with that, but how do I get it out that if I then only with two digits after the comma in the string will have the number?

    So
    string.Format (" % f ", num);
     
  4. creative

    creative New Member

    Joined:
    Feb 15, 2010
    Messages:
    87
    Likes Received:
    0
    Trophy Points:
    0
    1st looking times in the FAQ, then you knew more ...
    second the resolution of the should be at best int ...
    Code:
    int = float * 100; 
    float = int, 
    float / = 100; 
    CString:: format ("% f", float); 
     
  5. techme

    techme New Member

    Joined:
    Feb 15, 2010
    Messages:
    86
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    CString strFloat; / / Here is the float-variable inside 
    
    strFloat.SetAt ((strFloat.Find (' .') +2),' \ 0 ');
    The idea:
    I am looking for the point ('.'), So put the comma, and put two characters followed by a '\ 0', which ends the string
     
  6. inspiration

    inspiration New Member

    Joined:
    Feb 15, 2010
    Messages:
    85
    Likes Received:
    0
    Trophy Points:
    0
    Thanks creative, but that did not mean I have as far as ego already at Erhard understood.
    After your method is yes, then the string

    as: 12.560000
    , it is only 12:56 in the string are.

    and the method of techme I can not even use, because in String 3 floats each will stand with 2nachkommastellen.
     
  7. creative

    creative New Member

    Joined:
    Feb 15, 2010
    Messages:
    87
    Likes Received:
    0
    Trophy Points:
    0
    but then take only three strings, each with a float inside the pack-var and then together an easy-to-string
     
  8. techinspiration

    techinspiration New Member

    Joined:
    Feb 14, 2010
    Messages:
    54
    Likes Received:
    0
    Trophy Points:
    0
    Hello, try the following times:
    Code:
    float fValue = 0.047645645; 
    CString strAusgabe; 
    strAusgabe.Format (" % .2 f ", fValue);
    the .2 you lay by the precision fixed. In the fall she is 2 decimal places.
    The float is automatically rounded, ie strAusgabe contains the string "0.05"
     
  9. creative

    creative New Member

    Joined:
    Feb 15, 2010
    Messages:
    87
    Likes Received:
    0
    Trophy Points:
    0
    Uff! Do not something like! You have very bad problems later if you believe in what the string anhengen still want!
    Code:
    CString strWithNULL = " Hello "; 
    strWithNULL.Replace (" ! "," \ 0 "); 
    strWithNULL + = " hello "; 
    MessageBox (strWithNULL);
    The MessageBox is now Hello and nciht, as perhaps expected Hello, hello ...
     
  10. inspiration

    inspiration New Member

    Joined:
    Feb 15, 2010
    Messages:
    85
    Likes Received:
    0
    Trophy Points:
    0
    thanks everybody!

    :)
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice