rijinrv
rijinrv

Reputation: 2039

Java decimal part ignored

consider the code:

public double calculatePrice()
    { 
      int discount = 0 ;
      double totalPrice=itemCount*itemPrice;
      double netPrice;
      for(int i=0;i<NOOFITEMS.length;i++)
      {
        /*  if(i==0)
          {
              if(itemCount<NOOFITEMS[i])
               { discount=0;
                 break;
               }
          }
          /*if(i==NOOFITEMS.length-1)
          {  
        if(itemCount>NOOFITEMS[i])
           {  discount=50;
             break;
           }
          }
          if(itemCount<NOOFITEMS[i]&&itemCount>NOOFITEMS[i+1])
          {
            discount=DISCOUNTPERCENTAGE[i];  
          }*/
          discount=10;
      }
     System.out.println("discount "
             +discount);  
     System.out.println("totalprice "
             +totalPrice); 
      netPrice=totalPrice-(totalPrice*(discount/100));
      return netPrice;
    }

output::

Bill  ID:   501
Item NAME: grocery
 NO of Item: 11
 Price of Item: 250.0
discount 10
totalprice 2750.0
net price          2750.0
TRAINEE ID:   502
TRAINEE NAME: fruits
 Price of Item: 300.0
Item NO: 15
discount 10
totalprice 4500.0
net price         4500.0

The value of "netprice" should be 4050 [4500-(4500*(10/100)] as per the given statement.But the value is outputed as 4500 always.. the discount/100 is not computed correctly.Always the decimal part is ignored. Can't figure out the error..!

Upvotes: 0

Views: 578

Answers (4)

İsmet Alkan
İsmet Alkan

Reputation: 5447

When working with double type and you want the output to be double too, let all your variables be double instead of int. Operating ints will result ints. This, changing to double, will cause no problems as all int numbers are also double numbers.

Speaking for your example, change your variable "discount" to double:

double discount = 0;

Also, you're doing division by 100, and it's also an int, change it to double, too:

netPrice = totalPrice - (totalPrice * (discount / 100d));

Or to do a more secure operation, define variables for all your numbers to see them clearer:

double divider = 100;
netPrice = totalPrice - (totalPrice * (discount / divider));

Upvotes: 1

DrA
DrA

Reputation: 437

discount/100 is integer division. This will always be 0 when discount is less than 100.

I want to give a better answer. As a professor who assigns this problem in some of my Java classes, tehy way to find your coding errors...

When you have code that doesn't do what you expect. Set a break point and watch the behavior of every line of code. It sounds tedious, but it gets faster.

When you find the line that's doing something you don't expect, look at that line carefully. Sometimes you still can see it, and will need to break the line into smaller pieces. In this case you can do the math in multiple steps.

temp = discount / 100;

Then use temp in the longer expression. This is a better way to debug than asking for help from others. You will learn faster, and be a more confident programmer.

Upvotes: 3

MrSmith42
MrSmith42

Reputation: 10151

Try this:

netPrice=totalPrice-(totalPrice*(discount/100.));

You used integer arithmetics.

Upvotes: 0

assylias
assylias

Reputation: 328608

The culprit is:

totalPrice*(discount/100)

discount is an int, so discount/100 is an integer division and decimals get truncated. For example, 10/100 is 0.

To fix the issue, you can force the use of doubles:

totalPrice*(discount/100d)

Upvotes: 3

Related Questions