0 Replies - 268 Views - Last Post: 08 January 2010 - 04:50 AM

#1 Huzi94   User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 119
  • Joined: 14-November 09

Check if a number is decimal

Posted 08 January 2010 - 04:50 AM

Description: This snippet checks whether a number is decimal or not.
//This snippet checks if a number is decimal or not.
public class Decimal {
  public void run() {
    if(validate(-552)) {//if the number is a decimal then print success message
	    System.out.println("Number is a decimal");
	}
	else { // else print failure message.
	    System.out.println("Number is not a decimal");
	}
  }
  //Function to check if a number is decimal or not  
  public boolean validate(double v) {
	return (Math.floor(v) != v); //if the number is not a whole number then its a decimal.
  }
    public static void main(String args[]) {
      Decimal myDecimal = new Decimal();
	  myDecimal.run();//Run the program
    }
}


Is This A Good Question/Topic? 0
  • +

Page 1 of 1