Ok, wie runde ich eine Dezimalzahl in die 2. Dezimalstelle statt nur die erste Dezimalstelle? Ich hatte ein Menü zu erstellen, in dem das „Kunde“ Aufträge Essen und wir hatten die Zwischensumme und Steuern haben und enthalten Trinkgeld. Mein Code funktioniert gut, es sei denn die Gesamtsumme so etwas wie $ 3.8 ist, wenn es 3,80 $ sein sollte. Wie kann ich das beheben, indem ich nur Math.round() verwende? Mein Code für das Geld Teil des Menüs istRunde Dezimalstelle auf die zweite Dezimalstelle
double SUBtotal = subTotal * 100.00;
System.out.println("Your current total is: $" +
Math.round(SUBtotal)/100.00);
System.out.println("Options:");
System.out.println(" 1. Order another item");
System.out.println(" 2. Checkout");
mainOp = scan.nextInt();
if (mainOp==2)
{
System.out.println(Order);
double tax = subTotal * 0.0825;
double taxSubtotal = tax + subTotal;
double please = taxSubtotal * 100.00;
double taxSubtotal2 = Math.round(please)/100.00;
System.out.println("\nSubtotal (with tax): $ " + taxSubtotal2);
System.out.println("Tip: $");
double tip = scan.nextDouble();
double total = taxSubtotal + tip;
double total2 = total * 100.00;
double total3 = Math.round(total2) /100.00;
System.out.println("Final Total: $" + total3);
System.out.println();
System.out.println("Thank You For Ordering!");
}
Vielen Dank!
https://stackoverflow.com/questions/153724/how-to-round-a-number-to-n-decimal-places-in-java – user2173372
Bitte Ihre Frage nicht mutwillig zerstören. – LW001