Displaying zeros

This is a discussion on Displaying zeros within the C++ Programming forums, part of the General Programming Boards category; Hi I am fairly new to programming. Can anyone help me in getting it to display zeros to the left ...

  1. #1
    ATL
    ATL is offline
    Registered User
    Join Date
    Mar 2008
    Posts
    1

    Displaying zeros

    Hi

    I am fairly new to programming. Can anyone help me in getting it to display zeros to the left of a decimal point? For example, instead of displaying 5 show as 005?

  2. #2
    Registered User
    Join Date
    Jan 2008
    Posts
    58
    Set the fill for cout to be whatever character you want. Then when you have a width longer than the length of the value, empty space will be repaced with that character.
    Code:
    #include <iomanip>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        double a = 5.0;
        int b = 5;
    
        cout.fill('0');
        cout << setw(3) << a << endl;
        cout << setw(3) << b << endl;
    
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 04-12-2009, 06:49 PM
  2. leading zero's..... ( reading int )
    By sh4k3 in forum C Programming
    Replies: 4
    Last Post: 06-12-2007, 10:03 AM
  3. Need Help Displaying A Pattern.
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 10-05-2005, 12:01 PM
  4. Displaying all zeros?
    By musayume in forum C Programming
    Replies: 2
    Last Post: 02-04-2002, 08:51 PM
  5. Need codes for displaying figures in descending mode.
    By rbaba in forum C++ Programming
    Replies: 0
    Last Post: 12-14-2001, 08:47 PM

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