Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Groovy numbers


May 14, 2021 Groovy


Table of contents


In Groovy, numbers are actually represented as objects, and they are all instances of class Integer. To make an object do something, we need to call a method declared in its class.

Groovy supports integers and floats.

  • An integer is a value that does not contain a fraction.
  • Floating points are a hedding value that contains a small part.

An example of a number in Groovy looks like this -

Integer x = 5; 
Float y = 1.25; 

where x is the integer type and y is the floating point.

Numbers in Groovy are defined as objects usually because of the requirement to perform operations on numbers. The concept of providing a class on the original type is called a wrapper class.

By default, the following wrapper classes are available in Groovy.

Groovy numbers

The objects of the wrapper class contain or wrap their respective basic data types. ng. he process of converting an object back to its corresponding base type is called unboxing.

Example

The following are examples of boxing and unboxing

class Example { 
   static void main(String[] args) {
      Integer x = 5,y = 10,z = 0; 
		
      // The the values of 5,10 and 0 are boxed into Integer types 
      // The values of x and y are unboxed and the addition is performed 
      z = x+y; 
      println(z);
   }
}

The output of the above program will be 15. In the above example, the values of 5, 10, and 0 are embedded in the integer variables x, y, and z accordingly. /b10>The output of the above program will be 15. pe.

The digital method

Because Numbers in Groovy are represented as classes, the following is a list of available methods.

Serial number Method and description
1 xxxValue()

This method accepts Number as an argument and returns the base type based on the called method.

2 compareTo()

The compareTo method is to compare one number with another. This is useful if you want to compare the values of numbers.

3 equals()

The method determines whether the Number object that calls the method is equal to the object passed as an argument.

4 valueOf()

The valueOf method returns the relevant Number object that holds the value of the passed argument.

5 toString()

This method is used to get a String object that represents the value of the Number object.

6 parseInt()

This method is used to get the original data type of a String. ParseXxx() is a static method that can have one or two parameters.

7 abs()

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

8 ceil()

Method ceil gives a minimum integer greater than or equal to the argument.

9 floor()

Method floor gives the maximum integer that is less than or equal to the argument.

10 rint()

Method rint returns an integer with the nearest value to the argument.

11 round()

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

12 min()

The method gives the smaller of the two parameters. A rguments can be int, float, long, double.

13 max()

The method gives the maximum value of the two parameters. Arguments can be int, float, long, double.

14 exp()

The method returns that the base number of natural pair e is the power of the argument.

15 log()

The method returns the natural pair of parameters.

16 pow()

The method returns that the value of the first argument is increased to the power of the second argument.

17 sqrt()

The method returns the square root of the argument.

18 sin()

The method returns the sine value that specifies the double value.

19 cos()

The method returns the cosine value that specifies the double value.

20 tan()

The method returns a positive cut value that specifies the double value.

21 asin()

The method returns the antisine value that specifies the double value.

22 acos()

The method returns the anti-cosine value that specifies the double value.

23 atan()

The method returns the reverse cut that specifies the double value.

24 atan2()

The method converts the right-angle coordinates (x,y) to polar coordinates (r, theta) and returns the tata.

25 toDegrees()

This method converts parameter values to degrees.

26 radian()

This method converts parameter values to radians.

27 random()

This method is used to generate random numbers between 0.0 and 1.0. T he range is: 0.0 . Different ranges can be achieved by using arithmetic.