-2

i need to check whether the number input is in decimal format or in floating point format in java coding.

in simple terms how would this check be possible?

RS786
  • 1
  • here is one way to do that http://stackoverflow.com/a/38714547/43848 – artem Aug 23 '16 at 01:01
  • Possible duplicate of [How to check whether input value is integer or float?](http://stackoverflow.com/questions/4727569/how-to-check-whether-input-value-is-integer-or-float) – Loknar Aug 23 '16 at 01:42
  • 1
    what do you mean by decimal format..? – Ramesh-X Aug 23 '16 at 02:03
  • decimal format - a number that is either positive or negative but is to the base 10. for example 20. – RS786 Aug 23 '16 at 13:07

2 Answers2

0
Scanner scanner  = new Scanner(System.in);
if(scanner.hasNextDouble())
     //double stuff here
else if (scanner.hasNextFloat())
     //Float stuff here
Falla Coulibaly
  • 759
  • 2
  • 11
  • 23
0

Here is a small method I quickly whipped up that you may find interesting....

public static boolean isFloatingPoint(Object number) {
    String type = number.getClass().getSimpleName().toUpperCase();
    return type.equals("FLOAT") || type.equals("DOUBLE");
}

You can use the same concept to determine any Object passed to your method, perhaps even:

public static boolean isJButton(Object component) {
    String type = component.getClass().getSimpleName().toUpperCase();
    return type.equals("JBUTTON");
}
DevilsHnd - 退職した
  • 8,739
  • 2
  • 19
  • 22