-1

How to identify decimal portion value that exists with a float data type?
If the decimal part results as zero, exempting the decimal portion shall be a good practice. How this can be achieved when dealing with float values making it easeful?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Pinkesh
  • 59
  • 1
  • 8
  • 1
    Possible duplicate of [How know a variable type in java?](http://stackoverflow.com/questions/2674554/how-know-a-variable-type-in-java) – Josué Zatarain Jun 16 '16 at 13:33
  • What does that code have to do with the problem you are facing? Please post the code that shows that you have made an attempt at resolving you our problem. – Popeye Jun 16 '16 at 14:09

3 Answers3

0
inputValue.getClass().getName()

example: value: type = java.lang.String

inputValue.getClass().getSimpleName();

example: value: String

Josué Zatarain
  • 791
  • 6
  • 21
0

Your variables num1 and num2 are both floats not integers as mentioned in your question, I guess that what you want to achieve is to find a way to know if a given float has decimals or not and for this you can proceed as next:

float f = 46.0f;
if (f % 1.0 == 0.0){
    System.out.printf("float without decimal %.0f%n", f);
} else {
    System.out.printf("float with decimals %.2f%n", f);
}

Output:

float without decimal 46
Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
0

Ended up with the below solution.

if (Value - (int) Value) != 0)
   //Contains the decimal portion
else
   //Decimal portion doesn't exist  

Helps in identifying the type of value stored and avoid unnecessary usage of decimal portion if an int value.

Pinkesh
  • 59
  • 1
  • 8