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 ?
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 ?
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.
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 ?
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 ?
Pierre Maurette
Je cherche un moyen performant et élégant de transformer un nombre en texte, complété par des zéros.
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
Je cherche un moyen performant et élégant de transformer un nombre en texte,
complété par des zéros.
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;
}
}
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
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.
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
--------------------------------------------------------------- [...] même si tu comprends pas ce que je dis, tu le comprends (Jean-Claude Van Damme) ---------------------------------------------------------------
Pierre Maurette wrote:
Je cherche un moyen performant et élégant de transformer un nombre en
texte, complété par des zéros.
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
---------------------------------------------------------------
[...] même si tu comprends pas ce que je dis, tu le comprends
(Jean-Claude Van Damme)
---------------------------------------------------------------
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
--------------------------------------------------------------- [...] même si tu comprends pas ce que je dis, tu le comprends (Jean-Claude Van Damme) ---------------------------------------------------------------
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
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
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
Je cherche un moyen performant et lgant de transformer un nombre en
texte, complt par des zros.
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
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
JBB
Je cherche un moyen performant et lgant de transformer un nombre en texte, complt par des zros.
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.
Je cherche un moyen performant et lgant de transformer un nombre en
texte, complt par des zros.
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.
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.
TestMan
Je cherche un moyen performant et élégant de transformer un nombre en texte, complété par des zéros.
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
Je cherche un moyen performant et élégant de transformer un nombre en
texte, complété par des zéros.
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 ;-)
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
Eraser Head
TestMan wrote:
Je cherche un moyen performant et élégant de transformer un nombre en texte, complété par des zéros.
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) ---------------------------------------------------------------
TestMan wrote:
Je cherche un moyen performant et élégant de transformer un nombre en
texte, complété par des zéros.
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)
---------------------------------------------------------------
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) ---------------------------------------------------------------
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 lgant de transformer un nombre en texte, complt par des zros.
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.
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 lgant de transformer un nombre en
texte, complt par des zros.
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.
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 lgant de transformer un nombre en texte, complt par des zros.
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.