question about int format

Get answers to all your basic programming questions. No Ogre questions, please!
User avatar
nevarim
Gnoll
Posts: 675
Joined: Mon Jul 05, 2010 6:16 pm
Location: Pavia Italy
x 4

question about int format

Post by nevarim »

hi all

anybody has a script for to change format of an int to a Ogre::string changing digits? for those number under 10 so i can have


from to
0 00
1 01
2 02
3 03
4 04
5 05
6 06
7 07
8 08
9 09


thanks :)

Neva
i'm a noob until proven otherwise :D
used in my project ;) and thanks to everyone :D
Ogre 3d
Mygui
Skyx
Hydrax
MOC
CCS
User avatar
tod
Troll
Posts: 1394
Joined: Wed Aug 02, 2006 9:41 am
Location: Bucharest
x 94

Re: question about int format

Post by tod »

You can use sprintf or snprintf if available. Something like this.

Code: Select all

char temp[3];
sprintf(temp,"%02u");
Ogre::String str(temp);
C++ probably has another way also.
User avatar
masterfalcon
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 4270
Joined: Sun Feb 25, 2007 4:56 am
Location: Bloomington, MN
x 126

Re: question about int format

Post by masterfalcon »

Also see StringConverter::ParseInt
User avatar
nevarim
Gnoll
Posts: 675
Joined: Mon Jul 05, 2010 6:16 pm
Location: Pavia Italy
x 4

Re: question about int format

Post by nevarim »

parseInt is not from string to int?

i need inverse

from int to string

maybe i need this
static String Ogre::StringConverter::toString ( int val,
unsigned short width = 0,
char fill = ' ',
std::ios::fmtflags flags = std::ios::fmtflags(0)
)

with width=1 and char fill='0'
i'm a noob until proven otherwise :D
used in my project ;) and thanks to everyone :D
Ogre 3d
Mygui
Skyx
Hydrax
MOC
CCS
User avatar
codrer
Kobold
Posts: 29
Joined: Tue May 14, 2013 11:16 pm
Location: Strasbourg, France
x 26

Re: question about int format

Post by codrer »

Like this, simply?

Code: Select all

int i = 3 ;
Ogre::String a = digit(i) ; // will return "03"

Ogre::String digit( int i , int n = 2 )
{
	// if you need 3 digits like "002" call digit(i,3);

	Ogre::String r = Ogre::StringConverters::toString(i) ;
	while( r.length() < n )
	{	r = "0" + r ;
	}	
	return r ;
	
}