954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

too many digits past decimal

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

commando1200
Newbie Poster
4 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 


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

commando1200
Newbie Poster
4 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: