Results 1 to 4 of 4
Thread: using percentages?
- 09-29-2009, 11:50 PM #1
Member
- Join Date
- Sep 2009
- Posts
- 31
- Rep Power
- 0
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) ).
- 09-30-2009, 01:06 AM #3
Member
- Join Date
- Sep 2009
- Posts
- 31
- Rep Power
- 0
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 01:27 AM.
-
If you need a double input then read in a double -- input.nextDouble() -- and have a double variable accept this input:
Please look up the API for the java.util.Scanner class if you need to know more.Java Code:double myDouble = input.nextDouble();
Similar Threads
-
[SOLVED] Noobie - Calculating Percentages
By fullmetaljacket in forum New To JavaReplies: 16Last Post: 05-22-2009, 01:10 AM -
Percentages
By Ciwan in forum New To JavaReplies: 3Last Post: 02-16-2009, 05:34 AM -
Qadratic Formula and Percentages
By bbtgirl in forum JCreatorReplies: 4Last Post: 02-07-2009, 03:07 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks