JAVA BEGINNERS TUTORIAL – Dealing With Numbers 2

Back to number methods and more function to discuss 🙂

toString()

The method is used to get a String object representing the value of the Number Object. If the method takes a primitive data type as an argument, then the String object representing the primitive data type value is return. بطاقات لعب  If the method takes two arguments, then a String representation of the first argument in the radix specified by the second argument will be returned.

String toString()
static String toString(int i)
  • toString(): This returns a String object representing the value of this Integer.
  • toString(int i): This returns a String object representing the specified integer i.

Example:

public class Test{ 

   public static void main(String args[]){
      Integer x = 5;
      System.out.println(x.toString());  
      System.out.println(Integer.toString(12)); 
   }
}

parseInt()

This method is used to get the primitive data type of a certain String. parseXxx() is a static method and can have one argument or two.

  • replace Xxx with the datatype.
static int parseInt(String s)
static int parseInt(String s, int radix)
  • s — This is a string representation of decimal.
  • radix — This would be used to convert String s into integer.
  • parseInt(String s): This returns an integer (decimal only).
  • parseInt(int i): This returns an integer, given a string representation of decimal, binary, octal, or hexadecimal (radix equals 10, 2, 8, or 16 respectively) numbers as input.

Example:

public class Test{ 

   public static void main(String args[]){
      int x =Integer.parseInt("8");
      double c = Double.parseDouble("6");
      int b = Integer.parseInt("546",16);

      System.out.println(x);
      System.out.println(c);
      System.out.println(b);
   }
}

abs()

The method gives the absolute value of the argument. The argument can be int, float, long, double, short, byte.

double abs(double d)
float abs(float f)
int abs(int i)
long abs(long lng)
  • Any primitive data type
  • This method Returns the absolute value of the argument.

Example:

public class Test{ 

   public static void main(String args[]){
      Integer a = -8;
      double d = -100;
      float f = -90;    
						
      System.out.println(Math.abs(a));
      System.out.println(Math.abs(d));     
      System.out.println(Math.abs(f));    
   }
}

ceil()

The method ceil gives the smallest integer that is greater than or equal to the argument.

double ceil(double d)
double ceil(float f)
  • A double or float primitive data type
  • This method Returns the smallest integer that is greater than or equal to the argument. Returned as a double.

Example:

public class Test{ 

   public static void main(String args[]){
      double d = 10.583;
      float f = -701;    

      System.out.println(Math.ceil(d));
      System.out.println(Math.ceil(f));  
   }
}

floor()

The method floor gives the largest integer that is less than or equal to the argument.

double floor(double d)
double floor(float f)
  • A double or float primitive data type
  • This method Returns the largest integer that is less than or equal to the argument. Returned as a double.

Example:

public class Test{ 

   public static void main(String args[]){
      double d = 10.583;
      float f = -701;    
	 
      System.out.println(Math.floor(d));
      System.out.println(Math.floor(f));
   }
}

rint()

The method rint returns the integer that is closest in value to the argument.

double rint(double d)
  • d — A double primitive data type
  • This method Returns the integer that is closest in value to the argument. Returned as a double.

Example:

public class Test{ 

   public static void main(String args[]){
      double d = 10.6;
      double e = 10.5;
      double f = 10.4;

      System.out.println(Math.rint(d));
      System.out.println(Math.rint(e)); 
      System.out.println(Math.rint(f)); 
   }
}

round()

The method round returns the closest long or int, as given by the methods return type.

long round(double d)
int round(float f)
  • d — A double or float primitive data type
  • f — A float primitive data type
  • This method Returns the closest long or int, as indicated by the method’s return type, to the argument.

Example:

public class Test{ 

   public static void main(String args[]){
      double d = 100.675;
      double e = 100.500;
      float f = 100;
      float g = 90f;

      System.out.println(Math.round(d));
      System.out.println(Math.round(e)); 
      System.out.println(Math.round(f)); 
      System.out.println(Math.round(g)); 
   }
}

Comments on this post

No comments.

Leave a Reply

Your email address will not be published. Required fields are marked *

1 × 1 =

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Trackbacks and Pinbacks on this post

No trackbacks.

TrackBack URL