LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-16-2011, 01:21 AM   #1
chinho
Member
 
Registered: Dec 2010
Distribution: fedora, openSUSE
Posts: 49

Rep: Reputation: 1
How to format int 1 to string 0001 in C++?


Hi guys,

In C++ as subjected, can anyone throw me some light on how I can generic-ly format an integer value of 1 to a string value of 0001? 11 to 0011? and 111 to 0111?

simply by just appending 0 in front and limiting the length of the string to 4?

Much Thanks!
Chinho
 
Old 02-16-2011, 01:37 AM   #2
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
Maybe messing around with std::fixed and std::setprecision?
 
Old 02-16-2011, 01:42 AM   #3
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
I'm sorry, here is what you need to use:

Code:
int mynumber = 1;
cout << setfill('0') << setw(2) << mynumber; 
cout << setfill('0') << setw(4) << mynumber;
 
Old 02-16-2011, 02:23 AM   #4
chinho
Member
 
Registered: Dec 2010
Distribution: fedora, openSUSE
Posts: 49

Original Poster
Rep: Reputation: 1
Thanks corp!
 
Old 02-16-2011, 02:28 AM   #5
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
Not a problem at all, glad to see it worked for you. Thanks for asking too, it gives me something to do!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert string (C++) to int FreeDoughnut Programming 10 10-30-2006 03:26 PM
converting a int to string irfanhab Programming 6 07-30-2005 09:40 PM
int to string+sendto pantera Programming 3 08-19-2004 07:19 PM
string to int BadTaste Programming 11 08-18-2004 10:23 AM
string to int in C h/w Programming 2 12-05-2003 03:47 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration