-
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
-
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) ).
-
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.
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);
}
}
-
If you need a double input then read in a double -- input.nextDouble() -- and have a double variable accept this input:
Code:
double myDouble = input.nextDouble();
Please look up the API for the java.util.Scanner class if you need to know more.