[FIXED] So drucken Sie einschließlich 0 mit Float oder anderen Datentypen, da keine Null gedruckt wird

Ausgabe

Jedes Mal, wenn ich meinen Code drucke, wird die Ausgabe nicht vollständig 1,50 drucken, sondern nur 1,5.

Mein Code ist

float G1 = 1.00f;
float G2 = 1.50f;
float G3 = 2.00f;

dann

if (SGradeTotal == totalgrades) {
    System.out.println(StudentName + G1);
} else if (SGradeTotal >= 400) {
    System.out.println(StudentName + G2);
} else if (SGradeTotal >= 380) {
    System.out.println(StudentName + G3);

Die Ausgabe wird nur sein: 1,5 nicht 1,50. Ich möchte es komplett ausdrucken.

Lösung

Versuchen Sie die Formatierung mit String.format(“%.02f”, G1) anstelle von G1

if (SGradeTotal == totalgrades) {
    System.out.println(StudentName + String.format("%.02f", G1));
} else if (SGradeTotal >= 400) {
    System.out.println(StudentName + String.format("%.02f", G2));
} else if (SGradeTotal >= 380) {
    System.out.println(StudentName + String.format("%.02f", G3));
}


Beantwortet von –
Jefin Stephan


Antwort geprüft von –
Dawn Plyler (FixError Volunteer)

0 Shares:
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like