0

I am doing this Java question :

Jalaj purchased a table for Rs.200.4 and due to some scratches on its top he had to sell it for Rs.176.5. Find his loss and loss%.

LOSS : Cost Price - Selling Price

LOSS% : (LOSS/Cost Price)*100

a. Declare four double variables i.e. costprice, sellingPrice, loss, lossPercentage

b. Assign 200.4 to costPrice and 176.5 to sellingPrice.

c. Print loss and lossPercentage according to the given formulas like: 823.67 8.23 d.

For printing upto two decimal places use

System.out.printf("%.2f\n", loss);

Note:

  1. output for both should be upto two decimal places

  2. Output should be displayed in two different lines

My code:

/*Write your code here */
import java.util.Scanner;
class bkws{
    public static void main(String args[]){
        double costPrice,sellingPrice,loss,lossPercentage;
        costPrice=200.4;
        sellingPrice=176.5;
        loss=costPrice-sellingPrice;
        lossPercentage=(loss/costPrice)*100;
        System.out.print("%.2f",loss);
        System.out.println("%.2f",lossPercentage);
    }
}

Now I am thinking of using Math.round but for rounding off to 2 decimal places it should be:

Math.round(number*100)/100;

But it is giving error and I also want to know that if there is any easy way to round off to n decimal places without using Math.round in Java.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
utkarsh dubey
  • 141
  • 2
  • 14
  • 1
    @Unknown is there a duplicate for how to copy/paste code for your instructions ;) – Peter Lawrey Sep 12 '16 at 10:39
  • `@PeterLawrey` If you can help by `code` it will be better not by **words**! – utkarsh dubey Sep 12 '16 at 10:42
  • @utkarshdubey You have been give one line of code you need to use already. It's not clear to me why you chose to do something different even though it doesn't work. I repeated the code you need in my answer. – Peter Lawrey Sep 12 '16 at 10:44
  • It is giving error : – utkarsh dubey Sep 12 '16 at 10:46
  • `Line:/bkws.java:10: error: no suitable method found for print(java.lang.String,double) System.out.print("%.2f",loss);` ^ – utkarsh dubey Sep 12 '16 at 10:46
  • Side note: read about java naming conventions. Class names start UpperCase, **always**. And dont use abbreviations; use names that say what that thing is. A name like "ComputePercentageExample" says more than bkws, doesn't it?! – GhostCat Sep 12 '16 at 10:47
  • @utkarshdubey I have already explained in my answer they you are calling a different method with a different name which expects just one argument. If you can copy/paste the question why can't you copy and paste it into your code, why write it again incorrectly? – Peter Lawrey Sep 12 '16 at 10:47
  • just named `bkws` for fun .I already know naming conventions ! – utkarsh dubey Sep 12 '16 at 10:48

3 Answers3

2
            double costPrice,sellingPrice,loss,lossPercentage;
            costPrice=200.4;
            sellingPrice=176.5;
            loss=costPrice-sellingPrice;
            lossPercentage=(loss/costPrice)*100;
            System.out.println(String.format("%.2f", loss));
            System.out.println(String.format("%.2f", lossPercentage));

OR

            double costPrice,sellingPrice,loss,lossPercentage;
            costPrice=200.4;
            sellingPrice=176.5;
            loss=costPrice-sellingPrice;
            lossPercentage=(loss/costPrice)*100;
            loss = (int)Math.round(loss * 100)/(double)100;
            lossPercentage = (int)Math.round(lossPercentage * 100)/(double)100;
            System.out.println(loss);
            System.out.println(lossPercentage);
bhanu avinash
  • 484
  • 2
  • 16
0

printf with an f for format at the end is a different method to print or println

For printing upto two decimal places use

System.out.printf("%.2f\n", loss);

BTW: Using %n inside a format string is a better choice than \n as %n is platform independent.

You don't need to round the result if you using rounding in the format.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

The real lesson to be learned here: all of the things you need are clearly documented.

You can start here to learn about System.out; to then go forward and understand how such PrintStreams work in general.

You will also find nice explanations there how those "format" patterns actually work.

In other words: the one big thing about using Java is that A) there is a library method for almost everything and B) all of that is documented with extraordinary quality. There is no need to speculate; and heck: not even a need to ask other folks for such information. It is all there, waiting for you.

GhostCat
  • 137,827
  • 25
  • 176
  • 248