在Groovy中,数字实际上表示为对象,它们都是类Integer的一个实例。要使对象做某事,我们需要调用在其类中声明的一个方法。
Groovy支持整数和浮点数。
- 整数是不包含分数的值。
- 浮点数是包含小数部分的十进制值。
Groovy中的数字示例如下所示:
Integer x = 5;
Float y = 1.25;
其中x是整数类型,y是浮点数。
groovy中的数字被定义为对象的原因通常是因为存在对数字执行操作的要求。在原始类型上提供类的概念被称为包装类。
默认情况下,Groovy中提供了以下包装程序类。
包装类的对象包含或包装其各自的基本数据类型。将原始数据类型转换为对象的过程称为装箱,这由编译器负责。将对象转换回其对应的基本类型的过程称为取消装箱。
1、例子
以下是装箱和拆箱的例子:
class Example {
static void main(String[] args) {
Integer x = 5,y = 10,z = 0;
// 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);
}
}
上述程序的输出将为5.在上述示例中,5,10和0的值相应地首先嵌入到整数变量x,y和z中。上述程序的输出将是5。然后,当执行x和y的添加时,值从其整数类型取消装箱。
2、数字方法
由于Groovy中的Numbers表示为类,以下是可用的方法列表。
序号 | 方法和描述 |
---|---|
1 | xxxValue() 此方法接受Number作为参数,并基于调用的方法返回基本类型。 |
2 | compareTo() compareTo方法是使用比较一个数字与另一个数字。如果要比较数字的值,这是有用的。 |
3 | equals() 该方法确定调用方法的Number对象是否等于作为参数传递的对象。 |
4 | valueOf() valueOf方法返回保存所传递的参数的值的相关Number对象。 |
5 | toString() 该方法用于获取表示Number对象的值的String对象。 |
6 | parseInt() 此方法用于获取某个String的原始数据类型。 parseXxx()是一个静态方法,可以有一个参数或两个参数。 |
7 | abs() 该方法给出了参数的绝对值。参数可以是int,float,long,double,short,byte。 |
8 | ceil() 方法ceil给出大于或等于参数的最小整数。 |
9 | floor() 方法floor给出小于或等于参数的最大整数。 |
10 | rint() 方法rint返回值最接近参数的整数。 |
11 | round() 方法round返回最接近的long或int,由方法返回类型给出。 |
12 | min() 该方法给出两个参数中较小的一个。参数可以是int,float,long,double。 |
13 | max() 该方法给出了两个参数的最大值。参数可以是int,float,long,double。 |
14 | exp() 该方法返回自然对数e的底数为参数的幂。 |
15 | log() 该方法返回参数的自然对数。 |
16 | pow() 该方法返回第一个参数的值增加到第二个参数的幂。 |
17 | sqrt() 该方法返回参数的平方根。 |
18 | sin() 该方法返回指定double值的正弦值。 |
19 | cos() 该方法返回指定double值的余弦值。 |
20 | tan() 该方法返回指定double值的正切值。 |
21 | asin() 该方法返回指定double值的反正弦值。 |
22 | acos() 该方法返回指定double值的反余弦值。 |
23 | atan() 该方法返回指定double值的反正切。 |
24 | atan2() 该方法将直角坐标(x,y)转换为极坐标(r,theta),并返回theta。 |
25 | parseInt() 该方法将参数值转换为度。 |
26 | radian() 该方法将参数值转换为弧度。 |
27 | random() 该方法用于生成介于0.0和1.0之间的随机数。范围是:0.0 = <Math.random <1.0。通过使用算术可以实现不同的范围。 |
(1)Groovy xxxValue()方法
此方法接受Number作为参数,并基于调用的方法返回基本类型。 以下是可用的方法列表:
byte byteValue()
short shortValue()
int intValue()
long longValue()
float floatValue()
double doubleValue()
- 参数:无需参数。
- 返回值:返回值是根据调用的值函数返回的原始类型。
以下是方法值的用法示例。
class Example {
static void main(String[] args) {
Integer x = 5;
// Converting the number to double primitive type
println(x.doubleValue());
// Converting the number to byte primitive type
println(x.byteValue());
// Converting the number to float primitive type
println(x.floatValue());
// Converting the number to long primitive type
println(x.longValue());
// Converting the number to short primitive type
println(x.shortValue());
// Converting the number to int primitive type
println(x.intValue());
}
}
当我们运行上面的程序,我们将得到以下结果:
5.0
5
5.0
5
5
5
(2)Groovy compareTo()方法
compareTo方法是使用比较一个数字与另一个数字。如果要比较数字的值,这是有用的。
public int compareTo( NumberSubClass referenceName )
- 参数:referenceName - 这可以是字节,双精度,整数,浮点型,长整型或短整型。
- 返回值:如果整数等于参数,则返回0。如果整数小于参数,则返回-1。如果整数大于参数,则返回1。
下面是一个使用这个方法的例子:
class Example {
static void main(String[] args) {
Integer x = 5;
//Comparison against a Integer of lower value
System.out.println(x.compareTo(3));
//Comparison against a Integer of equal value
System.out.println(x.compareTo(5));
//Comparison against a Integer of higher value
System.out.println(x.compareTo(8));
}
}
当我们运行上面的程序,我们将得到以下结果:
1
0
-1
(3)Groovy equals()方法
该方法确定调用方法的Number对象是否等于作为参数传递的对象。
public boolean equals(Object o)
- 参数:o - 任何对象。
- 返回值:如果参数不为空,并且是同一类型且具有相同数值的对象,则方法返回True。
下面是一个使用这个方法的例子:
class Example {
static void main(String[] args) {
Integer x = 5;
Integer y = 10;
Integer z = 5;
//Comparison against an Integer of different value
System.out.println(x.equals(y));
//Comparison against an Integer of same value
System.out.println(x.equals(z));
}
}
当我们运行上面的程序,我们将得到以下结果:
false
true
(4)Groovy valueOf()方法
valueOf方法返回保存所传递的参数的值的相关Number对象。参数可以是原始数据类型,String等。
这个方法是一个静态方法。 该方法可以接受两个参数,其中一个是字符串,另一个是基数。
static Integer valueOf(int i)
static Integer valueOf(String s)
static Integer valueOf(String s, int radix)
- 参数:
- i - 将返回整数表示的整数。
- s - 将返回整数表示的字符串。
- radix - 这将用于基于传递的字符串来确定返回的Integer的值。
- 返回值
- valueOf(int i) - 这返回一个Integer对象,它保存指定原语的值。
- valueOf(String s) - 这返回一个Integer对象,其中包含指定字符串表示形式的值。
- valueOf(String s,int radix) - 这返回一个Integer对象,它保存指定字符串表示形式的整数值,并使用radix值进行解析。
下面是一个使用这个方法的例子:
class Example {
static void main(String[] args) {
int x = 5;
Double z = 15.56;
Integer xNew = Integer.valueOf(x);
println(xNew);
Double zNew = Double.valueOf(z);
println(zNew);
}
}
5
15.56
(5)Groovy toString()方法
该方法用于获取表示Number对象的值的String对象。
如果方法采用原始数据类型作为参数,则返回表示原始数据类型值的String对象。
如果方法接受两个参数,则将返回第二个参数指定的基数中的第一个参数的String表示形式。
String toString()
static String toString(int i)
- 参数:i - 将返回字符串表示形式的int。
- 返回值:
- toString() - 此函数返回一个String对象,表示 this Integer的值。
- toString(int i) - 这返回一个表示指定整数的String对象。
下面是一个使用这个方法的例子:
class Example {
static void main(String[] args) {
Integer x = 5;
System.out.println(x.toString());
System.out.println(Integer.toString(12));
}
}
当我们运行上面的程序,我们将得到以下结果:
5
12
(6)Groovy parseInt()方法
此方法用于获取某个String的原始数据类型。 parseXxx()是一个静态方法,可以有一个参数或两个参数。
static int parseInt(String s)
static int parseInt(String s, int radix)
- 参数:
- s - 这是十进制的字符串表示形式。
- radix - 这将用于将String s转换为整数。
- 返回值:
- parseInt(String s) - 这会返回一个整数(仅限十进制)。
- parseInt(int i) - 返回一个整数,给定十进制,二进制,八进制或十六进制(基数分别等于10,2,8或16)数字作为输入的字符串表示形式。
下面是一个使用这个方法的例子:
class Example {
static void main(String[] args) {
int x = Integer.parseInt("9");
double y = Double.parseDouble("5");
int z = Integer.parseInt("444",16);
System.out.println(x);
System.out.println(y);
System.out.println(z);
}
}
当我们运行上面的程序,我们将得到以下结果:
9
5.0
1092
(7)Groovy abs()方法
该方法给出了参数的绝对值。参数可以是int,float,long,double,short,byte。
double abs(double d)
float abs(float f)
int abs(int i)
long abs(long lng)
- 参数:任何基本数据类型。
- 返回值:此方法返回参数的绝对值。
以下是此方法的使用示例。
class Example {
static void main(String[] args) {
Integer a = -8;
double b = -100;
float c = -90;
System.out.println(Math.abs(a));
System.out.println(Math.abs(b));
System.out.println(Math.abs(c));
}
}
当我们运行上面的程序,我们将得到以下结果 -
8
100.0
90.0
(8)Groovy ceil()方法
方法ceil给出大于或等于参数的最小整数。
double ceil(double d)
double ceil(float f)
- 参数:双精度或浮点型基本数据类型。
- 返回值:此方法返回大于或等于参数的最小整数。返回为双精度。
下面是一个使用这个方法的例子:
class Example {
static void main(String[] args) {
double a = -100.675;
float b = -90;
System.out.println(Math.ceil(a));
System.out.println(Math.ceil(b));
}
}
当我们运行上面的程序,我们将得到以下结果 -
-100.0
-90.0
(9)Groovy floor()方法
方法floor给出小于或等于参数的最大整数。
double floor(double d)
double floor(float f)
- 参数:双精度或浮点型基本数据类型。
- 返回值:此方法返回小于或等于参数的最大整数。返回为双精度。
下面是一个使用这个方法的例子:
class Example {
static void main(String[] args) {
double a = -100.675;
float b = -90;
System.out.println(Math.floor(a));
System.out.println(Math.floor(b));
}
}
当我们运行上面的程序,我们将得到以下结果:
-101.0
-90.0
(10)Groovy rint()方法
方法rint返回值最接近参数的整数。
double rint(double d)
- 参数:d - 它接受一个double值作为参数。
- 返回值:此方法返回值最接近参数的整数。 返回为双精度。
下面是一个使用这个方法的例子:
class Example {
static void main(String[] args){
double d = 100.675;
double e = 100.500;
double f = 100.200;
System.out.println(Math.rint(d));
System.out.println(Math.rint(e));
System.out.println(Math.rint(f));
}
}
当我们运行上面的程序,我们将得到以下结果:
101.0
100.0
100.0
(11)Groovy round()方法
方法round返回最接近的long或int,由方法返回类型给出。
long round(double d)
int round(float f)
- 参数:
- d - A double or float primitive data type
- f - A float primitive data type
- 返回值:此方法返回参数中最接近的 long 或 int ,如方法的返回类型所示。
下面是一个使用这个方法的例子:
class Example {
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));
}
}
当我们运行上面的程序,我们将得到以下结果:
101
101
100
90
(12)Groovy min()方法
该方法给出两个参数中较小的一个。参数可以是int,float,long,double。
double min(double arg1, double arg2)
float min(float arg1, float arg2)
int min(int arg1, int arg2)
long min(long arg1, long arg2)
- 参数:此方法接受任何原始数据类型作为参数。
- 返回值:此方法返回两个参数中较小的一个。
下面是一个使用这个方法的例子:
class Example {
static void main(String[] args) {
System.out.println(Math.min(12.123, 12.456));
System.out.println(Math.min(23.12, 23.0));
}
}
当我们运行上面的程序,我们将得到以下结果:
12.123
23.0
(13)Groovy max()方法
该方法给出两个参数中较大的一个。参数可以是int,float,long,double。
double max(double arg1, double arg2)
float max(float arg1, float arg2)
int max(int arg1, int arg2)
long max(long arg1, long arg2)
- 参数:此方法接受任何原始数据类型作为参数。
- 返回值:此方法返回两个参数中较大的一个。
下面是一个使用这个方法的例子:
class Example {
static void main(String[] args) {
System.out.println(Math.max(12.123, 12.456));
System.out.println(Math.max(23.12, 23.0));
}
}
当我们运行上面的程序,我们将得到以下结果:
12.456
23.12
(14) Groovy exp()方法
该方法返回自然对数e的底数为参数的幂。
double exp(double d)
- 参数:d - 任何原始数据类型。
- 返回值:此方法返回自然对数的底,e,为参数的幂。
下面是一个使用这个方法的例子:
class Example {
static void main(String[] args) {
double x = 11.635;
double y = 2.76;
System.out.printf("The value of e is %.4f%n", Math.E);
System.out.printf("exp(%.3f) is %.3f%n", x, Math.exp(x));
}
}
当我们运行上面的程序,我们将得到以下结果:
The value of e is 2.7183
exp(11.635) is 112983.831
(15)Groovy log()方法
该方法返回参数的自然对数。
double log(double d)
- 参数:d - 任何原始数据类型。
- 返回值:此方法返回参数的自然对数。
下面是一个使用这个方法的例子:
class Example {
static void main(String[] args) {
double x = 11.635;
double y = 2.76;
System.out.printf("The value of e is %.4f%n", Math.E);
System.out.printf("log(%.3f) is %.3f%n", x, Math.log(x));
}
}
当我们运行上面的程序,我们将得到以下结果:
The value of e is 2.7183
log(11.635) is 2.454
(16)Groovy pow()方法
该方法返回第一个参数的值增加到第二个参数的幂。
double pow(double base, double exponent)
- 参数:
- base - Any primitive data type
- exponent - Any primitive data type
- 返回值:此方法返回第一个参数的值增加到第二个参数的幂。
下面是一个使用这个方法的例子:
class Example {
static void main(String[] args) {
double x = 11.635;
double y = 2.76;
System.out.printf("The value of e is %.4f%n", Math.E);
System.out.printf("pow(%.3f, %.3f) is %.3f%n", x, y, Math.pow(x, y));
}
}
当我们运行上面的程序,我们将得到以下结果:
The value of e is 2.7183
pow(11.635, 2.760) is 874.008
(17)Groovy sqrt()方法
该方法返回参数的平方根。
double sqrt(double d)
- 参数:d - 任何原始数据类型。
- 返回值:此方法返回参数的平方根。
下面是一个使用这个方法的例子:
class Example {
static void main(String[] args) {
double x = 11.635;
double y = 2.76;
System.out.printf("The value of e is %.4f%n", Math.E);
System.out.printf("sqrt(%.3f) is %.3f%n", x, Math.sqrt(x));
}
}
当我们运行上面的程序,我们将得到以下结果:
The value of e is 2.7183
sqrt(11.635) is 3.411
(18)Groovy sin()方法
该方法返回指定double值的正弦值。
double sin(double d)
- 参数:d - 双数据类型。
- 返回值:此方法返回指定double值的正弦值。
下面是一个使用这个方法的例子:
class Example {
static void main(String[] args) {
double degrees = 45.0;
double radians = Math.toRadians(degrees);
System.out.format("The value of pi is %.4f%n", Math.PI);
System.out.format("The sine of %.1f degrees is %.4f%n", degrees, Math.sin(radians));
}
}
当我们运行上面的程序,我们将得到以下结果:
The value of pi is 3.1416
The sine of 45.0 degrees is 0.7071
(19)Groovy cos()方法
该方法返回指定double值的余弦值。
double cos(double d)
- 参数:d - 此方法接受double数据类型的值。
- 返回值:此方法返回指定double值的余弦值。
下面是一个使用这个方法的例子:
class Example {
static void main(String[] args){
double degrees = 45.0;
double radians = Math.toRadians(degrees);
System.out.format("The value of pi is %.4f%n", Math.PI);
System.out.format("The cosine of %.1f degrees is %.4f%n", degrees, Math.cos(radians));
}
}
当我们运行上面的程序,我们将得到以下结果:
The value of pi is 3.1416
The cosine of 45.0 degrees is 0.7071
(20)Groovy tan()方法
该方法返回指定double值的正切值。
double tan(double d)
- 参数:d - 此方法接受double数据类型的值。
- 返回值:此方法返回指定double值的正切值。
下面是一个使用这个方法的例子:
class Example {
static void main(String[] args) {
double degrees = 45.0;
double radians = Math.toRadians(degrees);
System.out.format("The value of pi is %.4f%n", Math.PI);
System.out.format("The tangent of %.1f degrees is %.4f%n", degrees, Math.tan(radians));
}
}
当我们运行上面的程序,我们将得到以下结果:
The value of pi is 3.1416
The tangent of 45.0 degrees is 1.0000
(21)Groovy asin()方法
该方法返回指定double值的反正弦值。
double asin(double d)
- 参数:d - 此方法接受double数据类型的值。
- 返回值:此方法返回指定double值的反正弦值。
下面是一个使用这个方法的例子:
class Example {
static void main(String[] args) {
double degrees = 45.0;
double radians = Math.toRadians(degrees);
System.out.format("The value of pi is %.4f%n", Math.PI);
System.out.format("The arcsine of %.4f is %.4f degrees %n",
Math.sin(radians), Math.toDegrees(Math.asin(Math.sin(radians))));
}
}
当我们运行上面的程序,我们将得到以下结果:
The value of pi is 3.1416
The arcsine of 0.7071 is 45.0000 degrees
(22)Groovy acos()方法
该方法返回指定double值的反余弦值。
double acos(double d)
- 参数:d - 此方法接受double数据类型的值。
- 返回值:此方法返回指定double值的反余弦值。
下面是一个使用这个方法的例子:
class Example {
static void main(String[] args) {
double degrees = 45.0;
double radians = Math.toRadians(degrees);
System.out.format("The value of pi is %.4f%n", Math.PI);
System.out.format("The arccosine of %.4f is %.4f degrees %n",
Math.cos(radians), Math.toDegrees(Math.acos(Math.sin(radians))));
}
}
当我们运行上面的程序,我们将得到以下结果:
The value of pi is 3.1416
The arccosine of 0.7071 is 45.0000 degrees
(23)Groovy atan()方法
该方法返回指定double值的反正切。
double atan(double d)
- 参数:d - 此方法接受double数据类型的值。
- 返回值:此方法返回指定double值的反正切值。
下面是一个使用这个方法的例子:
class Example {
static void main(String[] args) {
double degrees = 45.0;
double radians = Math.toRadians(degrees);
System.out.format("The value of pi is %.4f%n", Math.PI);
System.out.format("The arctangent of %.4f is %.4f degrees %n", Math.cos(radians),
Math.toDegrees(Math.atan(Math.sin(radians))));
}
}
当我们运行上面的程序,我们将得到以下结果:
The value of pi is 3.1416
The arctangent of 0.7071 is 35.2644 degrees
(24)Groovy atan2()方法
该方法将直角坐标(x,y)转换为极坐标(r,theta),并返回theta。
double atan2(double y, double x)
- 参数:
- X - X co-ordinate in double data type
- Y - Y co-ordinate in double data type
- 返回值:此方法从极坐标(r,theta)返回theta。。
下面是一个使用这个方法的例子:
class Example {
static void main(String[] args){
double x = 45.0;
double y = 30.0;
System.out.println( Math.atan2(x, y) );
}
}
当我们运行上面的程序,我们将得到以下结果:
0.982793723247329
(25)Groovy Numbers parseInt()方法
该方法将参数值转换为度。
double toDegrees(double d)
- 参数:d - 双数据类型。
- 返回值:此方法返回双精度值。
下面是一个使用这个方法的例子:
class Example {
static void main(String[] args) {
double x = 45.0;
double y = 30.0;
System.out.println( Math.toDegrees(x) );
System.out.println( Math.toDegrees(y) );
}
}
当我们运行上面的程序,我们将得到以下结果:
2578.3100780887044
1718.8733853924698
(26)Groovy radian()方法
该方法将参数值转换为弧度。
double toRadians(double d)
- 参数:d - 双数据类型。
- 返回值:此方法返回双精度值。
下面是一个使用这个方法的例子:
class Example {
static void main(String[] args) {
double x = 45.0;
double y = 30.0;
System.out.println( Math.toRadians(x) );
System.out.println( Math.toRadians(y) );
}
}
当我们运行上面的程序,我们将得到以下结果:
0.7853981633974483
0.5235987755982988
(27)Groovy random()方法
该方法用于生成介于0.0和1.0之间的随机数。 范围是:0.0 = 数学, 1.0。 通过使用算术可以实现不同的范围。
static double random()
- 参数:这是一个默认方法,不接受参数。
- 返回值:此方法返回double。
下面是一个使用这个方法的例子:
class Example {
static void main(String[] args) {
System.out.println( Math.random() );
System.out.println( Math.random() );
}
}
当我们运行上面的程序,我们将得到以下结果:
0.0543333676591804
0.3223824169137166