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.

  • 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:

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.

  • 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:

abs()

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

  • Any primitive data type
  • This method Returns the absolute value of the argument.

Example:

ceil()

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

  • 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:

floor()

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

  • 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:

rint()

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

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

Example:

round()

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

  • 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: