|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Senior Member
Iscritto dal: Oct 2006
Città: milano
Messaggi: 1439
|
[JAVA] visualizzare float senza decimali se 0
Ciao a tutti, c'è modo di visulizzare un float come se fosse un int nel caso che le sue cifre decimali siano 0? Qualche esempio
input: 21.3 output: 21.3 input: 5.0 output: 5 |
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Oct 2007
Città: Padova
Messaggi: 4131
|
metodo String.format() di java.lang.String
Vedi i javadoc, c'è un link che rimanda ai javadoc di java.util.Formatter e spiega come specificare la "format string". Questo se ti serve ottenere quel float come una Stringa formattata (utile ad esempio per stamparlo su un file). Se vuoi solo ottenere un output formattato da stampare sullo standard output allora c'è System.out.printf(). Anche qui però devi specificare la "format string".
__________________
As long as you are basically literate in programming, you should be able to express any logical relationship you understand. If you don’t understand a logical relationship, you can use the attempt to program it as a means to learn about it. (Chris Crawford) Ultima modifica di banryu79 : 08-04-2010 alle 09:43. |
|
|
|
|
|
#3 |
|
Senior Member
Iscritto dal: Oct 2006
Città: milano
Messaggi: 1439
|
Ti ringrazio, avevo già trovato le pagine in questione ma mi viene un po' difficile capire come si usano. Devo usare il flag '0' o sbaglio?
Io ho fatto qualcosa come Codice:
System.out.printf("input: 21.0\noutput: %0f", 21.0);
|
|
|
|
|
|
#4 | |
|
Senior Member
Iscritto dal: Oct 2007
Città: Padova
Messaggi: 4131
|
Così:
Codice:
System.out.printf("input: 21.0\noutput: %.0f", 21.0);
Questo è la sintassi della "format string": Quote:
__________________
As long as you are basically literate in programming, you should be able to express any logical relationship you understand. If you don’t understand a logical relationship, you can use the attempt to program it as a means to learn about it. (Chris Crawford) Ultima modifica di banryu79 : 08-04-2010 alle 10:27. |
|
|
|
|
|
|
#5 |
|
Senior Member
Iscritto dal: Oct 2006
Città: milano
Messaggi: 1439
|
Ma così se il decimale non è zero me lo toglie uguale.
|
|
|
|
|
|
#6 |
|
Senior Member
Iscritto dal: Oct 2007
Città: Padova
Messaggi: 4131
|
Vero, scusa ho preso una svista.
Allora mi sa che devi farlo tu: non mi sembra sia possibile specificare un comportamento del genere (cioè di limitare la precisione a una sola cifra E di non stamparla nel caso sia 0). Ad esempio così (un'idea di massima): Codice:
public class FloatFormatter
{
public static void main(String[] args) {
float num = 21.0f;
System.out.println("num = "+ num);
System.out.println("formatted num = "+doCustomFormat(num, 1));
}
public static String doCustomFormat(float f, int precision) {
float intPart = Float.valueOf(f).intValue();
String formatString = (intPart==f) ? "%.0f" : "%."+precision+"f";
return String.format(formatString, f);
}
}
__________________
As long as you are basically literate in programming, you should be able to express any logical relationship you understand. If you don’t understand a logical relationship, you can use the attempt to program it as a means to learn about it. (Chris Crawford) Ultima modifica di banryu79 : 08-04-2010 alle 11:09. |
|
|
|
|
|
#7 | |
|
Member
Iscritto dal: Jan 2008
Città: roma
Messaggi: 296
|
Quote:
Codice:
DecimalFormat decimal = new DecimalFormat("##0.##"); //, symbols);
float f = 13.0f;
System.out.println(decimal.format(f));
|
|
|
|
|
|
|
#8 | |
|
Senior Member
Iscritto dal: Oct 2007
Città: Padova
Messaggi: 4131
|
Quote:
__________________
As long as you are basically literate in programming, you should be able to express any logical relationship you understand. If you don’t understand a logical relationship, you can use the attempt to program it as a means to learn about it. (Chris Crawford) |
|
|
|
|
|
|
#9 |
|
Senior Member
Iscritto dal: Oct 2006
Città: milano
Messaggi: 1439
|
Grazie a tutti
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 17:06.




















