Finding Decimal Place in java

prmccar

Junior Member
Mar 23, 2013
1
0
0
I am creating a program for my java class and I need to find a way to check a double and see if it has a decimal. Is there any way to do this. Here is the code from the program:
import java.util.Scanner;
import java.text.DecimalFormat;
public class MethodsSurprise {
public static void main(String[] args) {
menu();
}
public static void menu() {
Scanner input = new Scanner(System.in);
int choice;
System.out.println("\t1. run the program.");
System.out.println("\t2. end the program.");
choice = input.nextInt();

switch (choice) {
case 1:
input();
break;
case 2:
System.exit(0);
case 3:
System.out.println("The choices are 1 or 2.");
menu();
break;
}
}
public static double input() {
Scanner input = new Scanner(System.in);
double numberGuess;
System.out.println("Choose a number between 1 and 50.");
numberGuess = input.nextDouble();
randomNumber(numberGuess);
return(0);
}
public static double randomNumber(double numberGuess) {
int rNumber;
rNumber = (int)(Math.random() * 10) + 1;
calculations(numberGuess, rNumber);
return(0);
}
public static double calculations(double numberGuess, int rNumber) {
double answer;
double finalAnswer;
answer = (double) numberGuess / (int) rNumber;
finalAnswer = (double) answer / 2;
output(numberGuess, rNumber, answer, finalAnswer);
return(0);
}
public static double output(double numberGuess, int rNumber, double answer, double finalAnswer) {
System.out.println(numberGuess);
System.out.println(rNumber);
System.out.println(answer);
System.out.println(finalAnswer);
menu();
return(0);
}

}

I need it to find if there is a decimal in the number finalAnswer. Any ideas?
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,603
4,521
75
You mean you need to check whether the double "is" also an int? The outlines of a solution appear several times in your code.

Do they need you to consider 2.999999999 == 3 or not?
 

Broheim

Diamond Member
Feb 17, 2011
4,587
3
81
I only took a quick peek but why don't you just use modulus to see if there's a remainder?
 

Ancalagon44

Diamond Member
Feb 17, 2010
3,274
202
106
Cast the double to an int, and subtract the int from the original double. If there is any difference, it had a decimal portion.

I know pixel shaders have built in functions such as fract, which return the fractional portion of a number. I dont know if Java has something similar?
 

BrightCandle

Diamond Member
Mar 15, 2007
4,762
0
76
The problem is doubles can't represent every number accurately, its in the nature of doubles to make it impossible to tell if they are precisely an integer. I could easily input a double that would pass certain logic for looking like an integer under one implementation and not another. Instead of reading a double from the input you should read an integer, that will solve the problem in your program and not have you worrying about the problems with doubles.
 

iCyborg

Golden Member
Aug 8, 2008
1,344
61
91
I think in numerics it's customary to compare doubles to within some small constant. So instead of "x == y", you use "|x-y| < EPSILON"
Here, I'd just do "abs(d - round(d)) < EPSILON". And use round, not floor or ceil.
 

veri745

Golden Member
Oct 11, 2007
1,163
4
81
I am creating a program for my java class and I need to find a way to check a double and see if it has a decimal. Is there any way to do this. Here is the code from the program:
Code:
import java.util.Scanner;
import java.text.DecimalFormat;
public class MethodsSurprise {
	public static void main(String[] args) {
		menu();
	}
	public static void menu() {
		Scanner input = new Scanner(System.in);
		int choice;
		System.out.println("\t1. run the program.");
		System.out.println("\t2. end the program.");
		choice = input.nextInt();
		
		switch (choice) {
		case 1:
			input();
			break;
		case 2:
			System.exit(0);
		case 3:
			System.out.println("The choices are 1 or 2.");
			menu();
			break;
		}
	}
	public static double input() {
		Scanner input = new Scanner(System.in);
		double numberGuess;
		System.out.println("Choose a number between 1 and 50.");
		numberGuess = input.nextDouble();
		randomNumber(numberGuess);
		return(0);
	}
	public static double randomNumber(double numberGuess) {
		int rNumber;
		rNumber = (int)(Math.random() * 10) + 1;
		calculations(numberGuess, rNumber);
		return(0);
	}
	public static double calculations(double numberGuess, int rNumber) {
		double answer;
		double finalAnswer;
		answer = (double) numberGuess / (int) rNumber;
		finalAnswer = (double) answer / 2;
		output(numberGuess, rNumber, answer, finalAnswer);
		return(0);
	}
	public static double output(double numberGuess, int rNumber, double answer, double finalAnswer) {
		System.out.println(numberGuess);
		System.out.println(rNumber);
		System.out.println(answer);
		System.out.println(finalAnswer);
		menu();
		return(0);
	}

}

I need it to find if there is a decimal in the number finalAnswer. Any ideas?

Using 'code' tags makes it much more readable.
 

Leros

Lifer
Jul 11, 2004
21,867
7
81
I think in numerics it's customary to compare doubles to within some small constant. So instead of "x == y", you use "|x-y| < EPSILON"
Here, I'd just do "abs(d - round(d)) < EPSILON". And use round, not floor or ceil.

That's true. There is usually is an epsilon. I still like analyzing the bits. You're guaranteed to be right that way.