Thread: Preceding 0

  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
    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, 12:47 PM
  2. more debugging
    By rippascal in forum C++ Programming
    Replies: 8
    Last Post: 03-25-2002, 05:58 PM
  3. preceding zeros in int variables
    By AshFooYoung in forum C Programming
    Replies: 2
    Last Post: 09-23-2001, 02:28 PM