Groovy User Guide (5)

xiaoxiao2021-03-06  57

5, Groovy Math

l Groovy supports all Java Math classes and operations

l In order to make MATH operations in the script, the Groovy Math model supports a literary MATH operation.

l Default computing is used as a precise decimal (BigDecimal), such as:

1.1 0.1 == 1.2

Returns True, not False (not using float or double in Java)

(1) Digital text representation

l Groovy's decimal text represents an instance of java.math.bigDecimal, not a floating point type (Float or Double)

l Float and Double can create the suffix (f and d) method later

l The decimal form of the decimal is also supported, such as 12.3e-23

l hexadecimal and octal support, hexadecimal prefix 0x, octal prefix 0

l Integer types can be created using the suffix (i, l, and g) methods later, if not specifying the size of the size according to the value

l Digital type suffix text representation

_Type_

_SUFFIX_

_Biginteger_

G

_Long_

L

_INTEGER_

I

_BIGDECIMAL_

(default)

_Double_

Di

_Float_

Fly

l example:

Assert 42i == New Integer ("42");

Assert 123L == New long ("123");

Assert 2147483648 == New long ("2147483648"); // long type used, value to large for anteger

Assert 456g == New java.math.biginteger ("456");

Assert 123.45 == New java.math.bigDecimal ("123.45"); // default bigdecimal type used

Assert 1.200065D == New Double ("1.200065");

Assert 1.234F == New float ("1.234");

askERT 1.23E23D == New Double ("1.23E23");

(2) Math operation

L Groovy's Math implementation is very close to the practice of Java 1.5 BigDecimal Math model

l Java.lang.Number includes binary operations (except for division), in addition to the following table automatically converts parameter types

_BIGDECIMAL_

_Biginteger_

_Double_

_Float_

_Long_

_INTEGER_

_BIGDECIMAL_

BigDecimal

BigDecimal

Double

Double

BigDecimal

BigDecimal

_Biginteger_

BigDecimal

Biginteger

Double

Double

Biginteger

Biginteger

_Double_

Double

Double

Double

Doubledouble

Double

_Float_

Double

Double

Double

Double

Double

Double

_Long_

BigDecimal

Biginteger

Double

Double

Long

Long

_INTEGER_

BigDecimal

Biginteger

Double

Double

Long

Integer

l Note: Byte, Character, Short as an Integer type

(3) division

L Solution Operations "/" and "/ =" When there is a float or double type in the operand, the result is a double type; other cases, the result is a BigDecimal type

l BigDecimal type operation will do this:

BigDecimal.divide (BigDecimal Right, , bigdecimal.round_half_up)

Where is max (this.scale (), Right.Scale (), 10)

l example:

1/2 == New java.math.bigDecimal ("0.5");

1/3 == New java.math.bigdecimal ("0.333333333");

2/3 == New java.math.bigdecimal ("0.666666667");

l Integer division uses "/" and "/ =" operation, return to integer type

l Since "/" is the escape character in Java, use the need to escape in the string

"x = 8 // 3"

(4) Digital text representation syntax

IntegerLiteral:

Base10integerliteral

Hexintegerliteral

Octalintegerliteral

Base10integerliteral:

Base10Numeral IntegertypeSuffix (optional)

Hexintegerliteral:

HEXNUMERALALTYPESUFFIX (OPTIONAL)

Octalintegerliteral:

OctalNumeral IntegertypeSuffix (optional)

IntegertypeSuffix: One of

I I l L g g

Base10Numeral:

0

Nonzerodigit Digits (Optional)

Digits:

Digit

DIGITS DIGIT

DIGIT:

0

Nonzerodigit

Nonzerodigit: one of

/ 1 2 3 4 5 6 7 8 9

HEXNUMERAL:

0 x hexdigits

0 x hexdigits

HEXDIGITS:

HEXDIGIT

HEXDIGIT HEXDIGITS

HEXDIGIT: One of

0 1 2 3 4 5 6 7 8 9 A b C D e f A B C D e f

OctalNumeral:

0 Octaldigits

Octaldigits:

Octaldigit

Octaldigit Octaldigits

Octaldigit: one of

0 1 2 3 4 5 6 7

DecimalPointLiteral:

Digits. Digits ExponentPart (optional) DecimalTypeSuffix (optional)

DIGITS ExponentPart (optional) DECIMALTYPESUFFIX (OPTIONAL) DIGITS ExponentPart DecalogPArthPart DecalogPASUFFIX (Optional)

DIGITS ExponentPart (optional) DecimalTypeSuffix (Optional)

ExponentPart:

ExponentIndicator Signedinteger

Exponentindicator: One of

e e

Signedinteger:

Signopt Digits

Sign: One of Of

-

DecimalTypeSuffix: One of

F f D D g g

转载请注明原文地址:https://www.9cbs.com/read-114641.html

New Post(0)