Use Math.round() , Math.floor() , Math.ceil() methods.
System.out.println("\n" +f+ "\t" +Math.round(c)); // will round it to the nearest possible integer
Main Topics
Browse All TopicsIn the below program the output is coming with decimal numbers. I want to Truncate the decimal numbers in the output. How to do that?
/* WRITE AND TEST A JAVA PROGRAM TO CONVERT A GIVEN TEMPERATURE IN FAHRENHEIT
TO CELCIUS USING THE FOLLOWING CONVERSION FORMULA: F-32/1.8 AND DISPLAY
THE VALUES IS TABULAR FORM */
/* TEMPERATURE CONVERSION */
import java.io.DataInputStream;
class tempcon
{
public static void main(String args[])
{
int a;
float f=0.0f;
double c=0.0;
DataInputStream in=new DataInputStream(System.in)
try
{
System.out.println("%*%*%*
System.out.println("Enter the value of a");
a=Integer.parseInt(in.read
System.out.println(+a);
System.out.println("Enter the temperature in fahrenheit");
f=Float.valueOf(in.readLin
System.out.println(f);
System.out.println("fahr\t
System.out.println("*!*! \t *!*!*!*!*!*!*!*!");
for(int i=1;i<=a;i++)
{
c=(f-32)/1.8;
System.out.println("\n" +f+ "\t" +c);
f=f+1;
}
}
catch(Exception e)
{
}
}
}
%*%*%*%*%*%*%OUTPUT%*%*%*%
Enter the value of a
5
Enter the temperature in fahrenheit
48.0
fahr celcius
*!*! *!*!*!*!*!*!*!*!
48.0 8.88888888888889
49.0 9.444444444444445
50.0 10.0
51.0 10.555555555555555
52.0 11.11111111111111
%*%*%*%*%*%*%*%*%*%*%*%*%*
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Also, if you're looking to round to a certain decimal place, try multiplying the number by the number of places that you'd like to round to, using the Math.round() function, and then dividing by that number again. For example, to round the number 25.674 to 2 decimal places, do the following:
double b = 25.674*100;
Math.round(b);
b = b/100;
Similarly, to round to 3 decimal places:
double b = 25.6747*1000;
Math.round(b);
b=b/1000;
Business Accounts
Answer for Membership
by: yongsingPosted on 2003-08-22 at 02:28:01ID: 9201801
(1) Change:
;
System.out.println(f);
to
System.out.println((int)f)
(2) Change:
System.out.println("\n" +f+ "\t" +c);
to
System.out.println("\n" +f+ "\t" + (int)c);