programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Maths Problem

Ranch Hand
Posts: 82
Question one == what is Math.round
Question two == what is happening here >>
x = Math.round(x * 1000);
x = x / 1000;
****************************************************************************
import java.util.Scanner;
public class Exercise6 {

public static void main(String[] args)
{
Scanner in = new Scanner(System.in);

System.out.print("Input floating-point number: ");
double x = in.nextDouble();
System.out.print("Input floating-point another number: ");
double y = in.nextDouble();

x = Math.round(x * 1000);
x = x / 1000;

y = Math.round(y * 1000);
y = y / 1000;

if (x == y)
{
System.out.println("They are the same up to three decimal places");
}
else
{
System.out.println("They are different");
}
}
}

Marshal
Posts: 56600
172
What do you think the answers are? You can find out about the methods of the Math class here. There is a hint about your second question in the program.

Bartender
Posts: 572
9
Well, read the documentation: Math.round(a) rounds a to the nearest integer.
Also note that these two are identical:
So, i
If x=123.4567, then
Math.round(x * 1000) is equal to Math.round(123456.7) which is equal to 123457.
Then dividing that by 1000 gives (approximately) 123.457 which is different than 123.4567.

Note that I say approximately, since neither 1234567 nor 123.457 can be expressed exactly using a double.