OVH Cloud OVH Cloud

formatage

9 réponses
Avatar
JBB
Je cherche un moyen performant et élégant de transformer un nombre en
texte, complété par des zéros.

12345 -> "12345"
1234 -> "1234"
12 -> "0012"
1 -> "0001"


Voici ou j'en suis de mes recherches:
String format(int i)
{
String s= (new Integer(i)).toString();
while (s.length()<4)
{
s = "0" + s;
}
return s;
}
Ca ne me parait pas très performant.

Ou mieux:

String format(int i)
{
String s= (new Integer(i)).toString();
if (s.length()<4)
{
s = "0000".subString(s.length()) + s;
}
return s;
}

ce qui me parait plus performant.
Mais bon j'imagine qu'il y a de bien meilleurs solutions.
Il y a sûrement une classe dans Java spécialement prévue. laquelle ?

9 réponses

Avatar
Raphael Tagliani
s = ""+i; évite de créer un integer pour rien.
sinon, je ne connais pas de meilleure méthode.

JBB wrote:
Je cherche un moyen performant et élégant de transformer un nombre en
texte, complété par des zéros.

12345 -> "12345"
1234 -> "1234"
12 -> "0012"
1 -> "0001"


Voici ou j'en suis de mes recherches:
String format(int i)
{
String s= (new Integer(i)).toString();
while (s.length()<4)
{
s = "0" + s;
}
return s;
}
Ca ne me parait pas très performant.

Ou mieux:

String format(int i)
{
String s= (new Integer(i)).toString();
if (s.length()<4)
{
s = "0000".subString(s.length()) + s;
}
return s;
}

ce qui me parait plus performant.
Mais bon j'imagine qu'il y a de bien meilleurs solutions.
Il y a sûrement une classe dans Java spécialement prévue. laquelle ?


Avatar
Pierre Maurette
Je cherche un moyen performant et élégant de transformer un nombre en texte,
complété par des zéros.

12345 -> "12345"
1234 -> "1234"
12 -> "0012"
1 -> "0001"


Voici ou j'en suis de mes recherches:
String format(int i)
{
String s= (new Integer(i)).toString();
while (s.length()<4)
{
s = "0" + s;
}
return s;
}
Ca ne me parait pas très performant.

Ou mieux:

String format(int i)
{
String s= (new Integer(i)).toString();
if (s.length()<4)
{
s = "0000".subString(s.length()) + s;
}
return s;
}

ce qui me parait plus performant.
Mais bon j'imagine qu'il y a de bien meilleurs solutions.
Il y a sûrement une classe dans Java spécialement prévue. laquelle ?


Oui, certainement. Les spécialiste ne manqueront pas de nous
l'indiquer. Je vous propose, plus pour le fun que pour l'efficacité:

public final class test {
static final int MAX_LENGTH = 5;
static String[] zeros = {
"", "0", "00", "000", "0000"};

public static void main(String[] argv) {
int[] tablo = {0, 1, 12, 123, 1234, 12345};
for(int i = 0; i < tablo.length; i++){
System.out.println(format(tablo[i]));
}
}

static String format(int i) {
String s = (new Integer(i)).toString();
return zeros[MAX_LENGTH - s.length()] + s;
}
}

--
Pierre Maurette

Avatar
Eraser Head
Pierre Maurette wrote:
Je cherche un moyen performant et élégant de transformer un nombre en
texte, complété par des zéros.

12345 -> "12345"
1234 -> "1234"
12 -> "0012"
1 -> "0001"


Voici ou j'en suis de mes recherches:
String format(int i)
{
String s= (new Integer(i)).toString();
while (s.length()<4)
{
s = "0" + s;
}
return s;
}
Ca ne me parait pas très performant.

Ou mieux:

String format(int i)
{
String s= (new Integer(i)).toString();
if (s.length()<4)
{
s = "0000".subString(s.length()) + s;
}
return s;
}

ce qui me parait plus performant.
Mais bon j'imagine qu'il y a de bien meilleurs solutions.
Il y a sûrement une classe dans Java spécialement prévue. laquelle ?


Oui, certainement. Les spécialiste ne manqueront pas de nous l'indiquer.
Je vous propose, plus pour le fun que pour l'efficacité:

public final class test {
static final int MAX_LENGTH = 5;
static String[] zeros = {
"", "0", "00", "000", "0000"};

public static void main(String[] argv) {
int[] tablo = {0, 1, 12, 123, 1234, 12345};
for(int i = 0; i < tablo.length; i++){
System.out.println(format(tablo[i]));
}
}

static String format(int i) {
String s = (new Integer(i)).toString();
return zeros[MAX_LENGTH - s.length()] + s;
}
}



Bon, v'la la mienne :

public static final int LEFT_PADDING = 1;
public static final int RIGHT_PADDING = 2;

public static String textPad( String text,
char padding,
int length,
int type )
{
StringBuffer sb1;

int i1;

if ( text == null ) {
return null;
}

i1 = text.length( );

if ( i1 >= length ) {
return text;
} // end if

if ( type == RIGHT_PADDING ) {
sb1 = new StringBuffer( text );
} // end if
else {
sb1 = new StringBuffer( );
} // end else

for ( ; length > i1; length-- ) {
sb1.append( padding );
} // end for

if ( type == LEFT_PADDING ) {
sb1.append( text );
} // end if

return sb1.toString( );
} // end textPad()


Et voilà comment l'appeler :

machaine = textPad(String.valueOf(12), '0', 4, RIGHT_PADDING);

Bon courage pour la suite

--

GilloS

---------------------------------------------------------------
[...] même si tu comprends pas ce que je dis, tu le comprends
(Jean-Claude Van Damme)
---------------------------------------------------------------


Avatar
Kupee
s = ""+i; évite de créer un integer pour rien.
sinon, je ne connais pas de meilleure méthode.


Ben Integer.toString(int) est plus propre je trouve

Avatar
Real Gagnon
Je cherche un moyen performant et ‚l‚gant de transformer un nombre en
texte, compl‚t‚ par des z‚ros.

12345 -> "12345"
1234 -> "1234"
12 -> "0012"
1 -> "0001"


import java.text.DecimalFormat;
public class F {
public static void main(String args[]) {
long n = 123456;
String mask = "00000000000";
// jdk1.1
DecimalFormat df = new DecimalFormat(mask);
System.out.println(df.format(n));
}
}

Bye.
--
Real Gagnon from Quebec, Canada
* Looking for Java or PB code examples ? Visit Real's How-to
* http://www.rgagnon.com/howto.html

Avatar
JBB
Je cherche un moyen performant et ‚l‚gant de transformer un nombre en
texte, compl‚t‚ par des z‚ros.

12345 -> "12345"
1234 -> "1234"
12 -> "0012"
1 -> "0001"


import java.text.DecimalFormat;
public class F {
public static void main(String args[]) {
long n = 123456;
String mask = "00000000000";
// jdk1.1
DecimalFormat df = new DecimalFormat(mask);
System.out.println(df.format(n));
}
}

Bye.
Voila qui est très élégant. C'est la classe que je cherchais.

Par contre à l'usage cela s'avère moins performant.


Avatar
TestMan
Je cherche un moyen performant et élégant de transformer un nombre en
texte, complété par des zéros.

12345 -> "12345"
1234 -> "1234"
12 -> "0012"
1 -> "0001"


Voici ou j'en suis de mes recherches:
String format(int i)
{
String s= (new Integer(i)).toString();
while (s.length()<4)
{
s = "0" + s;
}
return s;
}
Ca ne me parait pas très performant.

Ou mieux:

String format(int i)
{
String s= (new Integer(i)).toString();
if (s.length()<4)
{
s = "0000".subString(s.length()) + s;
}
return s;
}

ce qui me parait plus performant.
Mais bon j'imagine qu'il y a de bien meilleurs solutions.
Il y a sûrement une classe dans Java spécialement prévue. laquelle ?


Bonjour,

Essayez :
String chaine = String.format("%04d", i );

Plus de détails sur :
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Formatter.html

Aucune idée des perfs, mais c'est ma soluce préférée ;-)

A+
TM

Avatar
Eraser Head
TestMan wrote:
Je cherche un moyen performant et élégant de transformer un nombre en
texte, complété par des zéros.

12345 -> "12345"
1234 -> "1234"
12 -> "0012" 1 -> "0001"


Voici ou j'en suis de mes recherches:
String format(int i)
{
String s= (new Integer(i)).toString();
while (s.length()<4)
{
s = "0" + s;
}
return s;
}
Ca ne me parait pas très performant.

Ou mieux:

String format(int i)
{
String s= (new Integer(i)).toString();
if (s.length()<4)
{
s = "0000".subString(s.length()) + s;
}
return s;
}

ce qui me parait plus performant.
Mais bon j'imagine qu'il y a de bien meilleurs solutions.
Il y a sûrement une classe dans Java spécialement prévue. laquelle ?


Bonjour,

Essayez :
String chaine = String.format("%04d", i );

Plus de détails sur :
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Formatter.html

Aucune idée des perfs, mais c'est ma soluce préférée ;-)

A+
TM


Pas mal cette façon de faire aussi, c'est propre et élégant et en plus
ça me rappelle le bon temps du C ;-)

Je la remise par devers moi !

--

GilloS

---------------------------------------------------------------
[...] même si tu comprends pas ce que je dis, tu le comprends
(Jean-Claude Van Damme)
---------------------------------------------------------------


Avatar
Mike Baroukh
Voila qui est très élégant. C'est la classe que je cherchais.
Par contre à l'usage cela s'avère moins performant.


Ce qu'il ne faut pas, c'est créer le DecimalFormat à chaque fois ...
L'ennui, c'est que ces classes (DecimaFormat, DateFormat, ...) ne sont
pas threadSafe.

Do,nc, si tu est en web, le mieux est d'utiliser un threadLocal pour les
avoir toujours de pret.
Si c'est une appli Swing, met le en static.

Mike

Je cherche un moyen performant et ‚l‚gant de transformer un nombre en
texte, compl‚t‚ par des z‚ros.

12345 -> "12345"
1234 -> "1234"
12 -> "0012" 1 -> "0001"


import java.text.DecimalFormat;
public class F {
public static void main(String args[]) {
long n = 123456;
String mask = "00000000000";
// jdk1.1
DecimalFormat df = new DecimalFormat(mask);
System.out.println(df.format(n));
}
}

Bye.
Voila qui est très élégant. C'est la classe que je cherchais.

Par contre à l'usage cela s'avère moins performant.