Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2014
    Posts
    4
    Rep Power
    0

    Default Percentage of a Double

    Very basic question , here..

    Java Code:
    double Compact=30.50;
    double Full=40.50;
    double discount=.07;
    double FullDiscount= 37.67;
    double CompactDiscount= 28.37;
    My question is how do I configure this so that when / if I need to change the discount value, it automatically adjusts the % discount off of the Full or Compact?

    All I can get to work is simply hardcoding it and assigning the discount value, i'm probably just messing up the syntax.

    Thanks for your help!
    Last edited by chaoticcircuit; 02-14-2014 at 06:19 PM.

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,524
    Rep Power
    5

    Default Re: Percentage of a Double

    Do you mean something like this?

    Java Code:
    double Compact=30.50;
    double Full=40.50;
    double discount=.07;
    double FullDiscount= Full * (1-discount);
    double CompactDiscount= Compact* (1-discount);
    Note that in the above case:

    1. It does not work when you change the value during program execution.
    2. Your precision will not be limited to two decimal places.

    So you might want to encode this in a method where you can do some rounding (which has its own set of problems). Best to work in cents (US) or the minimal unit of currency in other locales. Then convert to dollars or euros, or whatever.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Percentage of a Double

    Your variable names suggest that you're dealing with money. You should read this: Java Practices -> Representing money
    Get in the habit of using standard Java naming conventions!

  4. #4
    Join Date
    Feb 2014
    Posts
    4
    Rep Power
    0

    Default Re: Percentage of a Double

    Quote Originally Posted by jim829 View Post
    Do you mean something like this?

    Java Code:
    double Compact=30.50;
    double Full=40.50;
    double discount=.07;
    double FullDiscount= Full * (1-discount);
    double CompactDiscount= Compact* (1-discount);
    Note that in the above case:

    1. It does not work when you change the value during program execution.
    2. Your precision will not be limited to two decimal places.

    So you might want to encode this in a method where you can do some rounding (which has its own set of problems). Best to work in cents (US) or the minimal unit of currency in other locales. Then convert to dollars or euros, or whatever.

    Regards,
    Jim
    Ironically enough, I figured this out before I checked back, but thanks very much Jim. This is exactly what I needed, as basic as that is. I used %.2f to keep it to only 2 decimals.
    Last edited by chaoticcircuit; 02-16-2014 at 11:03 PM.

Similar Threads

  1. Percentage in Java
    By threlot in forum New To Java
    Replies: 1
    Last Post: 11-13-2012, 09:43 AM
  2. Call Different Task Based on their Percentage
    By raihan26 in forum Advanced Java
    Replies: 4
    Last Post: 05-16-2012, 06:04 AM
  3. Transfer file get percentage complete
    By LG87 in forum Networking
    Replies: 5
    Last Post: 06-16-2011, 09:04 PM
  4. Load percentage of an image
    By trishtren in forum Java 2D
    Replies: 0
    Last Post: 04-15-2011, 02:02 PM
  5. Percentage/decimal points?
    By Exhonour in forum New To Java
    Replies: 6
    Last Post: 01-16-2009, 10:35 PM

Posting Permissions

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