Im trying to make a tax and tip calculator and whenever the tax comes up it goes 10 or more decimal places and im trying to restrict it to two.

public static void main(String[] args)
    {
        double cost, total, tip, taxtotal, tiptotal, subtotal, grandtotal;
        Scanner myScanner = new Scanner(System.in);
        System.out.print("How much was your meal before tax? $");  //asks the user how much the meal was on the menu
        subtotal = myScanner.nextDouble();
        total = (subtotal * 1.09);  //adding NH food and meals tax of 9% to the total
        taxtotal = (subtotal * 0.09); //will show how much tax is paid
        System.out.print("What percentage would you like to tip? ");
        tip = myScanner.nextDouble();
        tiptotal = (subtotal * (tip / 100)); //the user can chose how much they want to tip on a percentage if flat number they put 0
        grandtotal = (total + tiptotal);
        System.out.println("The cost of your meal was $" + subtotal + ". You paid $" + taxtotal + " in taxes. You will be tipping " + tip + "% for a $" + tiptotal + " in tips making the grand total $" + grandtotal);
    }

any help would be appreciated thanks

Dani AI

Generated

Good quick find by — Math.round(value*100.0)/100.0 will trim extra digits but doesn't force two trailing decimals when printing (so 0.80 becomes 0.8). Two practical paths avoid that visual issue and improve correctness for money amounts: format the output for display, or do the math with a decimal type (or integer cents) and then format.

To force two decimals when printing (no change to calculation):

System.out.printf("Tax: $%.2f, Tip: $%.2f, Total: $%.2f%n", taxtotal, tiptotal, grandtotal);

For flexible patterns (always show two digits) use DecimalFormat:

DecimalFormat df = new DecimalFormat("0.00");
System.out.println("Tax: $" + df.format(taxtotal));

Or use the locale-aware currency formatter:

NumberFormat nf = NumberFormat.getCurrencyInstance();
System.out.println(nf.format(taxtotal));

For financial accuracy (recommended for real money calculations), avoid binary double for sums/taxes. Use BigDecimal (construct from user input string, not from a double) and round to two cents with an explicit RoundingMode:

BigDecimal subtotalBD = new BigDecimal(scanner.next());           // read as string
BigDecimal taxRate = new BigDecimal("0.09");
BigDecimal taxtotalBD = subtotalBD.multiply(taxRate).setScale(2, RoundingMode.HALF_UP);
BigDecimal grandTotalBD = subtotalBD.add(taxtotalBD);

Note: decide where to round — round each component (tax, tip) to cents before adding if the goal is to match receipts; otherwise round only at final display. Also import java.math.BigDecimal, java.math.RoundingMode, java.text.DecimalFormat or java.text.NumberFormat as needed.

found solution my appologies but if it helps

value1 = Math.round(value1*100.0)/100.0;

it wont force it to be 2 decimal places if the number ends up being .80 it will show .8 not a big deal but works for me

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.