PDA

View Full Version : [JAVA] convertire int in stringa a 2 cifre


Barbalbero
11-05-2008, 09:43
Domanda semplice
Il problema è che se faccio

int n1 = 2;
int n2 = 11;
String s1 = "" + n1;
String s2 = "" + n2;

allora s1 == "2" e s2 == "11"

ma io vorrei che s1 fosse "02" e s2 == "11"

TuX2K6
11-05-2008, 11:32
Provato con la classe NumberFormat (http://java.sun.com/j2se/1.4.2/docs/api/java/text/NumberFormat.html)? ;)

Don[ITA]
11-05-2008, 11:44
int n1 = 2;
int n2 = 11;
String s1 = "" + n1;
String s2 = "" + n2;
s1 = s1.length() < 2 ? "0" + s1 : s1;
s2 = s2.length() < 2 ? "0" + s2 : s2;


ciauz ;)