Converting integers into floats
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Converting integers into floats

  1. #1
    Join Date
    Dec 2010
    Posts
    583

    Converting integers into floats

    Just a quick question I've long forgotten, is that how do you convert integers into floats
    I remember there is something like ftoi, I am looking into something handy like the std
    Thanks
    Jack

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,245

    Re: Converting integers into floats

    You could use a static_cast or add 0.0f
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,273

    Re: Converting integers into floats

    Quote Originally Posted by lucky6969b View Post
    Just a quick question I've long forgotten, is that how do you convert integers into floats
    Doesn't assignment operator work?
    Or a simple C-casting?
    Victor Nijegorodov

  4. #4
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,699

    Re: Converting integers into floats

    Do you need rounding or truncation?

    EDIT: Sorry, pointless question I was thinking float to int
    Last edited by JohnW@Wessex; March 15th, 2012 at 04:00 AM.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  5. #5
    Join Date
    Jul 2005
    Posts
    19

    Re: Converting integers into floats

    static_cast we generally use for this. Well there can be multiple choice to do this as suggested in earlier posts


    regards,
    Vatsa
    www.objectiveprogramming.com

  6. #6
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Converting integers into floats

    Code:
    int i = 5;
    float f = i;
    All you should have to do is that. The compiler might warn you about the implicit conversion, but it should work.

  7. #7
    Join Date
    Dec 2010
    Posts
    583

    Re: Converting integers into floats

    Thanks guys, i've found a solution
    f = static_cast<float>(i);
    Have a nice day!!
    Jack

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center