Results 1 to 4 of 4
  1. #1
    shroomiin is offline Member
    Join Date
    Sep 2009
    Posts
    31
    Rep Power
    0

    Default using percentages?

    Hello everyone. I am trying to write a program that reads a percent discount and then deducts that from a total price. i was just wondering how do i assign percentages to a variable?

    what i am doing is

    print "please enter percent discount";
    percent discount++

    then be able to use that variable (which is a percentage) later on in the code. thanks

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    This isn't really programming but basic math and as such, you may do well to read up on percents, say on Wikipedia. If you reduce a price by 12%, you would subtract from the price the price * 12/100 which is the same as multiplying the price by 0.88 (which equals ((100 - 12) / 100) ).

  3. #3
    shroomiin is offline Member
    Join Date
    Sep 2009
    Posts
    31
    Rep Power
    0

    Default

    thanks! with your advice i was able to come up with a pretty good code. my only problem is i need to have the ability to input a number with a thousandths place into the baseprice variable, such as $305.68 opposed to just plain old $305. care to lead me i the right direction? this is what i have for my code. works well minus that small problem.

    Java Code:
    import java.util.Scanner;
    public class project2 {
    	public static void main(String[] args) {
    		
    		Scanner input = new Scanner(System.in);
    		
    		int baseprice;
    		int discountrate;
    		double discountamt;
    		double price2;
    		int tax;
    		double taxamt;
    		double finalprice;
    		
    		System.out.print("Please enter the base price of the vehicle: $");
    		baseprice = input.nextInt();
    		
    		System.out.print("Please enter the rate of the discount: %");
    		discountrate = input.nextInt();
    		
    		System.out.print("Please enter the rate of tax: %");
    		tax = input.nextInt();
    			
    		discountamt = baseprice * discountrate/100;
    		
    		price2 = baseprice - discountamt;
    		
    		taxamt = price2 * tax/100;
    		
    		finalprice = price2 * tax/100 + price2;
    		
    		if (finalprice >= 30000)
    			System.out.printf("Valued Customer Bonus!\n");
    		
    		System.out.printf("The amount of discount is: $%.2f\n", discountamt);
    		System.out.printf("The amount of tax is: $%.2f\n", taxamt);
    		System.out.printf("The final price of the car is: $%.2f\n", finalprice);
    		
    	}
    
    }
    Last edited by shroomiin; 09-30-2009 at 02:27 AM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    If you need a double input then read in a double -- input.nextDouble() -- and have a double variable accept this input:

    Java Code:
    double myDouble = input.nextDouble();
    Please look up the API for the java.util.Scanner class if you need to know more.

Similar Threads

  1. [SOLVED] Noobie - Calculating Percentages
    By fullmetaljacket in forum New To Java
    Replies: 16
    Last Post: 05-22-2009, 02:10 AM
  2. Percentages
    By Ciwan in forum New To Java
    Replies: 3
    Last Post: 02-16-2009, 06:34 AM
  3. Qadratic Formula and Percentages
    By bbtgirl in forum JCreator
    Replies: 4
    Last Post: 02-07-2009, 04:07 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •