|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Senior Member
Iscritto dal: Nov 2001
Città: Roma
Messaggi: 493
|
[Java] Formati numerici
Ho un piccolo problema con la formattazione di un numero.Lo vorrei nel formato XX.XX in quanto rappresenta delle percentuali che volgio approssimare alla seconda cifra decimale.
Ho scoperto la classe NumberFormat e mi sono creato un pattern come da documentazione: DecimalFormat formato = new DecimalFormat(); String pattern = "#,##0.0;(#,##0.0)"; //il pattern da applicare formato.applyPattern(pattern); try { out.println(formato.parse(formato.format(mio_numero))); } catch (ParseException e) {} dove mio_numero è un Double (oggetto Double non tipo primitivo..) Quello che vorrei è che dopo il punto mi comparisse lo zero nel caso non ci siano cifre decimali (50 --> 50.0) e da come ho impostato il pattern dovrebbe essere così....eppure non va,in caso il numero sia intero non rispetta il pattern..... Suggerimenti?
__________________
Listen the noise of deep sea --Powered by Debian Sid/unstable on 2.6.17.11-- |
|
|
|
|
|
#2 |
|
Member
Iscritto dal: Dec 2001
Città: Bari.
Messaggi: 209
|
Prova con questo...
Codice:
import java.text.DecimalFormat;
import java.text.ParseException;
public class Main {
public static void main(String[] args) {
DecimalFormat formato = new DecimalFormat();
String pattern = "0.0#";
formato.applyPattern(pattern);
Double mio_numero = new Double(0.0);
System.out.println(formato.format(mio_numero));
mio_numero = new Double(50);
System.out.println(formato.format(mio_numero));
mio_numero = new Double(50.0);
System.out.println(formato.format(mio_numero));
mio_numero = new Double(50.01);
System.out.println(formato.format(mio_numero));
mio_numero = new Double(50.014);
System.out.println(formato.format(mio_numero));
mio_numero = new Double(50.016);
System.out.println(formato.format(mio_numero));
mio_numero = new Double(150.0);
System.out.println(formato.format(mio_numero));
}
}
__________________
Se il giudice fosse giusto, forse il criminale non sarebbe colpevole - Dostoevskij |
|
|
|
|
|
#3 |
|
Senior Member
Iscritto dal: Nov 2001
Città: Roma
Messaggi: 493
|
Grazie perfetto così funziona
__________________
Listen the noise of deep sea --Powered by Debian Sid/unstable on 2.6.17.11-- |
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 11:23.


















