Preceding 0

This is a discussion on Preceding 0 within the C++ Programming forums, part of the General Programming Boards category; How can i get a preceding 0 in one digit numbers? Going to use it in a clock... I know ...

  1. #1
    Registered User morbuz's Avatar
    Join Date
    Aug 2001
    Posts
    35

    Question Preceding 0

    How can i get a preceding 0 in one digit numbers?
    Going to use it in a clock...
    I know there is a function that can do this...
    [Signature here. (Remove this!)]

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    If it's just for output you can take your pick between these two -

    Code:
    #include <iostream>
    #include <iomanip>
    #include <cstdio>
    using namespace std;
    
    int main()
    {
    
    	int i =5;
    
    	cout <<setfill('0')<<setw(2)<< i << endl;
    	printf("%02d\n",i);
    
    	return 0;
    }
    If you want to store the number with a preceding zero as a character then you can use stringstream or sprintf.

  3. #3
    Registered User morbuz's Avatar
    Join Date
    Aug 2001
    Posts
    35

    Thumbs up Thanks!

    Thanks a lot!
    Just what I was looking for
    [Signature here. (Remove this!)]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class Include Problem (I Think)
    By gpr1me in forum C++ Programming
    Replies: 8
    Last Post: 03-21-2006, 11:47 AM
  2. more debugging
    By rippascal in forum C++ Programming
    Replies: 8
    Last Post: 03-25-2002, 04:58 PM
  3. preceding zeros in int variables
    By AshFooYoung in forum C Programming
    Replies: 2
    Last Post: 09-23-2001, 02:28 PM

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