Numbers rounding off

This is a discussion on Numbers rounding off within the C++ Programming forums, part of the General Programming Boards category; When I try to display a number like .02, it shows as 0.0. How can I fix that? Thanks...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    204

    Numbers rounding off

    When I try to display a number like .02, it shows as 0.0. How can I fix that? Thanks

  2. #2
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    What code are you using to display it?
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    204
    Code:
    std::cout << .02 << std::endl;

  4. #4
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    I've really gotta stay out of the C++ forum (I know nothing about these commands)

    But some quick tutorial searches on Google and testing showed me that:
    Code:
    #include <iostream>
    using namespace std;
    
    
    int main(void)
    {
    
    	std::cout <<.02<< std::endl;
    
    	return 0;
    }
    Displays:
    0.02
    Press any key to continue

    And
    Code:
    #include <iostream>
    using namespace std;
    
    
    int main(void)
    {
    
    	std::cout <<".02"<< std::endl;
    
    	return 0;
    }
    Displays:
    .02
    Press any key to continue

    So, to answer your question, I don't know what the problem is.

    EDIT:
    The difference is with the quotation marks in the cout line.
    Last edited by Epo; 07-04-2005 at 08:30 PM.
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

  5. #5
    Banned
    Join Date
    Jun 2005
    Posts
    594
    Try this, your answer should be on the 3rd page of the pdf,


    Formatting Stream Outputs

  6. #6
    *this
    Join Date
    Mar 2005
    Posts
    498
    Or...

    you could simply do

    Code:
    cout << .02f << endl;
    Code:
    cout << (float).02 << endl;
    Code:
    cout << static_cast<float>(.02) << endl;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  2. Newbie question: rounding numbers
    By cantore in forum C Programming
    Replies: 10
    Last Post: 02-04-2006, 02:24 PM
  3. (C++) How would you go about rounding numbers?
    By jeffcoulter in forum C++ Programming
    Replies: 5
    Last Post: 09-19-2005, 07:47 AM
  4. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 10:15 AM
  5. Rounding Numbers accurately
    By crystaldawn68 in forum C++ Programming
    Replies: 5
    Last Post: 10-06-2001, 02:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21