2010-04-24 10 views

risposta

29

Prova questo:

String formattedNumber = String.format("%08d", number); 
+0

Ogni volta che viene chiamata una nuova istanza formater i s creato. Quindi devo ammettere che con grandi quantità di dati può causare grossi problemi di memoria. –

10

È possibile anche utilizzare la classe DecimalFormat, in questo modo:

NumberFormat formatter = new DecimalFormat("00000000"); 
System.out.println(formatter.format(100)); // 00000100 
2

String.format utilizza una stringa di formato che è descritto here

0

Questo funziona anche :

int i = 53; 
String spaceHolder = "00000000"; 
String intString = String.valueOf(i); 
String string = spaceHolder.substring(intString.lenght()).contract(intString); 

Ma gli altri esempi sono molto più semplici.

3

Ancora un altro modo. ;)

int x = ... 
String text = (""+(500000000 + x)).substring(1); 

-1 => 99999999 (nove complemento)

import java.util.concurrent.Callable; 
/* Prints. 
String.format("%08d"): Time per call 3822 
(""+(500000000+x)).substring(1): Time per call 593 
Space holder: Time per call 730 
*/ 
public class StringTimer { 
    public static void time(String description, Callable<String> test) { 
     try { 
      // warmup 
      for(int i=0;i<10*1000;i++) 
       test.call(); 
      long start = System.nanoTime(); 
      for(int i=0;i<100*1000;i++) 
       test.call(); 
      long time = System.nanoTime() - start; 
      System.out.printf("%s: Time per call %d%n", description, time/100/1000); 
     } catch (Exception e) { 
      System.out.println(description+" failed"); 
      e.printStackTrace(); 
     } 
    } 

    public static void main(String... args) { 
     time("String.format(\"%08d\")", new Callable<String>() { 
      int i =0; 
      public String call() throws Exception { 
       return String.format("%08d", i++); 
      } 
     }); 
     time("(\"\"+(500000000+x)).substring(1)", new Callable<String>() { 
      int i =0; 
      public String call() throws Exception { 
       return (""+(500000000+(i++))).substring(1); 
      } 
     }); 
     time("Space holder", new Callable<String>() { 
      int i =0; 
      public String call() throws Exception { 
       String spaceHolder = "00000000"; 
       String intString = String.valueOf(i++); 
       return spaceHolder.substring(intString.length()).concat(intString); 
      } 
     }); 
    } 
} 
+1

+1 per la creatività, anche se soluzione lenta :-). –

+1

Tuttavia, non funziona con i negativi. – polygenelubricants

+0

Potrebbe non essere lento come altre soluzioni e il comportamento non è definito per i negativi. Proprio come le altre soluzioni, ci sarà un non DDDDDDDD, dove D è una cifra, uscita per numeri negativi. –

0

Se avete bisogno di analizzare questa stringa supporto eo i18n possibilità di estendere l'oggetto

java.text.Format 

. Usa le altre risposte per aiutarti a ottenere il formato.

1

Se avete solo bisogno di stamparlo, questa è una versione più breve:

System.out.printf("%08d\n", number); 
2

Se Google Guava è un opzione:

String output = Strings.padStart("" + 100, 8, '0'); 

alternativa Apache Commons Lang:

String output = StringUtils.leftPad("" + 100, 8, "0"); 
Problemi correlati