Groovy用户指南(5)

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

5、Groovy Math

l Groovy支持访问所有的Java Math类和操作

l 为了使math操作在脚本编写时尽可能直观,Groovy math模型支持文字化math操作

l 缺省计算使用的是精确的小数(BigDecimal),如:

1.1 + 0.1 == 1.2

返回的是true,而不是false(不象在Java中使用float或double)

(1)数字的文字表示

l Groovy的小数文字表示是java.math.BigDecimal的实例,而不是浮点类型(Float或Double)

l Float和Double可以使用后面讲的后缀(F和D)方法来创建

l 小数的指数形式也支持,如12.3e-23

l 十六进制和八进制也支持,十六进制前缀0x,八进制前缀0

l 整数类型可以使用后面讲的后缀(I、L和G)方法来创建,如果不指定根据数值的大小使用合适的类型

l 数字类型的后缀文字表示

_Type_

_Suffix_

_BigInteger_

G

_Long_

L

_Integer_

I

_BigDecimal_

(缺省)

_Double_

D

_Float_

F

l 例子:

assert 42I == new Integer("42");

assert 123L == new Long("123");

assert 2147483648 == new Long("2147483648"); //Long type used, value too large for an Integer

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");

assert 1.23E23D == new Double("1.23E23");

(2)Math操作

l Groovy的Math实现很接近Java 1.5 BigDecimal Math模型的实践

l Java.lang.Number包括其子类的二元操作(除了除法)会根据下表自动转换参数类型

_BigDecimal_

_BigInteger_

_Double_

_Float_

_Long_

_Integer_

_BigDecimal_

BigDecimal

BigDecimal

Double

Double

BigDecimal

BigDecimal

_BigInteger_

BigDecimal

BigInteger

Double

Double

BigInteger

BigInteger

_Double_

Double

Double

Double

Double

Double

Double

_Float_

Double

Double

Double

Double

Double

Double

_Long_

BigDecimal

BigInteger

Double

Double

Long

Long

_Integer_

BigDecimal

BigInteger

Double

Double

Long

Integer

l 注意:Byte、Character、Short都作为Integer类型

(3)除法

l 除法操作“/”和“/=”在操作数中有Float或Double类型时,结果为Double类型;其它情况,结果为BigDecimal类型

l BigDecimal类型的操作会这样做:

BigDecimal.divide(BigDecimal right, <scale>, BigDecimal.ROUND_HALF_UP)

其中<scale>是MAX(this.scale(), right.scale(), 10)

l 例子:

1/2 == new java.math.BigDecimal("0.5");

1/3 == new java.math.BigDecimal("0.3333333333");

2/3 == new java.math.BigDecimal("0.6666666667");

l 整型除法使用“\”和“\=”操作,返回整型类型

l 由于“\”是Java中的转义符,在字符串中使用需要转义

" x = 8\\3 "

(4)数字文字表示语法

IntegerLiteral:

Base10IntegerLiteral

HexIntegerLiteral

OctalIntegerLiteral

Base10IntegerLiteral:

Base10Numeral IntegerTypeSuffix (optional)

HexIntegerLiteral:

HexNumeral IntegerTypeSuffix (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 DecimalTypeSuffix (optional)

Digits ExponentPart (optional) DecimalTypeSuffix (optional)

ExponentPart:

ExponentIndicator SignedInteger

ExponentIndicator: one of

e E

SignedInteger:

Signopt Digits

Sign: one of

+ -

DecimalTypeSuffix: one of

f F d D g G

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航