vgsrikanth
asked on
How to check if a input number is a integer or decimal in Java
Hello, I have a input number and I want to use a "if-statement" to check if a number is integer or decimal of the input number in Java. Can someone help me with the code.
In what form is the input - presumably a String? If so, either way you can call Double.parseDouble
do you mean that you want to check if a person typed decimal point:
then you can do that
String s = txtField.getText().trim();
if(s.indexOf(".") > -1){
// do soemthing with decimal
}
else
{
//do something with integer
}
Or you want to know if a person entered exactly integer number, like 5.0 for example ?
of course you can aslo check Integer.parseInt() after that to make sure they entered number at all
then you can do that
String s = txtField.getText().trim();
if(s.indexOf(".") > -1){
// do soemthing with decimal
}
else
{
//do something with integer
}
Or you want to know if a person entered exactly integer number, like 5.0 for example ?
of course you can aslo check Integer.parseInt() after that to make sure they entered number at all
ASKER
Actually I am doing this opetation (int)a/b where a and b are integers. I want to output the result in integer. if a/b results in integer number then I want to output it as it is otherwise I want to do (int)a/b+1
if a/b results in integer number
It always will be if both operands are of type int
ASKER
if a/b results in decimal then I want to do (int)a/b+1 otherwise just print the result. How do I code in Java
maybe something like that:
if( Math.abs(a/b - Math.floor(a/b)) > 0) {
int i = (int) a/b + 1;
}else
{
int i = ((int) a/b) ;
}
if( Math.abs(a/b - Math.floor(a/b)) > 0) {
int i = (int) a/b + 1;
}else
{
int i = ((int) a/b) ;
}
Sounds like you want
Math.ceil(Double.parseDouble(a)/Double.parseDouble(b))
int i = 0;
double a = 8.1;
double b = 2.0;
if( Math.abs(a/b - Math.floor(a/b)) > 0) {
i = (int) (a/b) + 1;
}else
{
i = (int) (a/b) ;
}
Output:
5
int i = 0;
double a = 8.0;
double b = 2.0;
if( Math.abs(a/b - Math.floor(a/b)) > 0) {
i = (int) (a/b) + 1;
}else
{
i = (int) (a/b) ;
}
System.out.println("i:" + i);
Output:
i:4
ASKER
Thank you Yan. I tried the above code with a=1000 and b=3 and I am getting 333 but I should get 334 since 1000/3 is a decimal.
int i = 0;
double a = 1000;
double b = 3;
if( Math.abs(a/b - Math.floor(a/b)) > 0) {
i = (int) (a/b) + 1;
}else
{
i = (int) (a/b) ;
}
System.out.println("i:" + i);
i:334
but I should get 334
(which is what you get with the code i posted)
Are you sure that you start with double values of 1000 and 3
If theye are ints then you'lll not get it
If theye are ints then you'lll not get it
if you are starting with strings you sould parse them to double, not to int also
ASKER
Yan, is there a code if a and b are integers
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Output
i:334
is there a code if a and b are integers
As i mentioned earlier, you need to treat them as double whether or not they're integers
ASKER
Thank you Yan and CEHJ for all the help. Yan your code is really helpful.
You are always welcome.
ASKER
Yan is really helpful and helps a lot with his code.
Why would you choose that overly verbose and unclear code??
Plus, why did you ignore the code i posted earlier at http:#37601893 ?
Plus, why did you ignore the code i posted earlier at http:#37601893 ?