Click here to Skip to main content
15,904,817 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want output to be corrected to 2 decimal points

Op:
Price: 9999.99 rs only

What I have tried:

System.out.println("enter the price");
double price=sc.nextDouble();

System.out.println("%.2f",price);
Posted
Updated 12-Apr-20 11:31am
Comments
Richard MacCutchan 23-Jan-20 8:12am    
What is the question?

 
Share this answer
 
v2
Try also:
Java
System.out.printf("%.2f\n",price);

which uses printf to format the output.
 
Share this answer
 
Comments
Yaswanth Kummar 23-Jan-20 11:41am    
I want output to be printed as following
Price: 9999.99 rs only
Richard MacCutchan 23-Jan-20 11:45am    
        System.out.printf("Price: %.2f Rs\n",price);
Hello, format or printf statements are used for such things like rounding decimals. Although you seem to be on the right track with your rounding, you need to use either format or print instead of just print or println because the program wouldn't read it and instead show an error message.
Try:
System.out.format("%.2f",price);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900