Click to See Complete Forum and Search --> : How do you round numbers?


Beavis
September 23rd, 1999, 10:30 PM
I am having a problem in trying to round the answer of a calculation...the program asks the user to enter the number of sprockets on 3 gears...then it asks for the ratio in relation to each other...in this case...the ratio of 2 to 1, 3 to 2, and 3 to 1...this is a sample of what i have...

float gear1;
float gear2;
float gear3;

System.out.print("Please enter the number of sprokets of the gear 1...");
gear1 =
Float.valueOf(inputStream.readLine().trim()).floatValue();

System.out.print("Please enter the number of sprokets of the gear 2...");
gear2=
Float.valueOf(inputStream.readLine().trim()).floatValue();

System.out.print("Please enter the number of sprokets of the gear 3...");
gear3=
Float.valueOf(inputStream.readLine().trim()).floatValue();

System.out.println("The number of sprokets on gear 1 is " + gear1);
System.out.println("The number of sprokets on gear 2 is " + gear2);
System.out.println("The number of sprokets on gear 3 is " + gear3);

float x1;
float x2;
float x3;

x1 = (float)(gear1 / gear2);
x2 = (float)(gear2 / gear3);

Math.round((x1 * 100) / 100.0);
Math.round((x2 * 100) / 100.0);

float xRatio2To1;
float xRatio3To1;

xRatio2To1 = (float)(gear1 / gear2);
xRatio3To1 = (float)(gear1 / gear3);

Math.round((xRatio2To1 * 100) / 100.0);
Math.round((xRatio3To1 * 100) / 100.0);

System.out.println("The gear ratio between gear 2 and gear 1 is " + Math.round((x1 * 100) / 100.0));
System.out.println("and " + Math.round((xRatio2To1 * 100) / 100.0));

System.out.println("The gear ratio between gear 3 and gear 2 is " + Math.round((x2 * 100) / 100.0));
System.out.println("and " + Math.round((xRatio3To1 * 100) / 100.0));

Beavis
September 23rd, 1999, 11:59 PM
You can't have 0.1 of a sprocket on a gear rite? what should i do then?...set them as int?

but then if i set them as int...when 2 is divided by 3...java would think its equal to 0...not 0.666667

n e insights?