Link to home
Start Free TrialLog in
Avatar of vgsrikanth
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.
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Avatar of vgsrikanth
vgsrikanth

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
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) ;
}
Sounds like you want

Math.ceil(Double.parseDouble(a)/Double.parseDouble(b))

Open in new window

            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) ;

}

Open in new window


Output:

5

Open in new window

            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);

Open in new window


Output:

i:4

Open in new window

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);

Open in new window


i:334

Open in new window

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 you are starting with strings you sould parse them to double, not to int also
Yan, is there a code if a and b are integers
ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Output
i:334

Open in new window

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
Thank you Yan and CEHJ for all the help. Yan your code is really helpful.
You are always welcome.
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 ?