Java aggiungere gli zeri iniziali per un numero
-
23-09-2019 - |
Domanda
Ho bisogno di restituire una stringa nella forma xxx-xxxx (dove xxx è un numero xxxx è un altro numero, tuttavia quando ho zeri iniziali scompaiono.Sto cercando il numero formatter, ma non funziona.
public String toString(){
NumberFormat nf3 = new DecimalFormat("#000");
NumberFormat nf4 = new DecimalFormat("#0000");
if( areaCode != 0)
return nf3.format(areaCode) + "-" + nf3.format(exchangeCode) + "-" + nf4.format(number);
else
return exchangeCode + "-" + number;
}
}
Ho capito:
public String toString(){
NumberFormat nf3 = new DecimalFormat("000");
NumberFormat nf4 = new DecimalFormat("0000");
if( areaCode != 0)
//myFormat.format(new Integer(someValue));
return nf3.format(new Integer(areaCode)) + "-" + nf3.format(new Integer(exchangeCode)) + "-" + nf4.format(new Integer(number));
else
return nf3.format(new Integer(exchangeCode)) + "-" + nf4.format(new Integer(number));
}
Soluzione
Quando AreaCode è 0, si dimentica di chiamare format
! Oltre a questo, si guarda bene. Il principale "#" non sono necessari, ma non causerà alcun problema per gli ingressi validi.
Ho appena provato fuori reale veloce per controllare e ha funzionato bene per me.
public static String formatTest(int areaCode, int exchangeCode, int number) {
DecimalFormat nf3 = new DecimalFormat("#000");
DecimalFormat nf4 = new DecimalFormat("#0000");
if( areaCode != 0)
return nf3.format(areaCode) + "-" + nf3.format(exchangeCode) + "-" + nf4.format(number);
else
return nf3.format(exchangeCode) + "-" + nf4.format(number);
}
public static void main(String[] args) {
System.out.println(formatTest(12, 90, 8));
System.out.println(formatTest(1, 953, 1932));
}
Output:
012-090-0008
001-953-1932
Altri suggerimenti
C'è una soluzione senza dubbio più elegante:
String.format("%03d-%03d-%04d", areaCode, exchangeCode, number)
Rimuovi il segno #
http://java.sun.com/docs/ libri / tutorial / i18n / formato / decimalFormat.html
Il codice:
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class Test
{
public static void main(String[] args)
{
int areaCode = 123;
int exchangeCode = 456;
NumberFormat nf3 = new DecimalFormat("0000");
System.out.println(nf3.format(areaCode) + "-" + nf3.format(exchangeCode) );
}
}
produce questo risultato:
0123-0456
Mi consiglia di utilizzare NumberFormat (http://docs.oracle.com/javase/7/docs/api/java/text/NumberFormat.html)
A mio parere la migliore leggibilità.E, inoltre, riduce al minimo la possibilità di errori quando si mette una Corda sbagliata nel DecimalFormat.
final int value1 = 1;
final double value2 = 4.2;
NumberFormat nf = NumberFormat.getInstance(Locale.US);
nf.setMinimumIntegerDigits(2);
System.out.println(nf.format(value1));
System.out.println(nf.format(value2));
Output:
01
04.2
(Il Locale è facoltativo ma mi raccomando quando si lavora con un team internazionale.Valore di Default sono le impostazioni locali)
Comunque NumberFormat in questo modo è una gran cosa, soprattutto se hai a che fare con paesi diversi o cose come percentuale o valuta