Groovy用户指南(5)

5Groovy Math

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

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

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

1.1 + 0.1 == 1.2
    
    

返回的是true,而不是false(不象在Java中使用floatdouble

1)数字的文字表示

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

l         FloatDouble可以使用后面讲的后缀(FD)方法来创建

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

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

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

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

2Math操作

l         GroovyMath实现很接近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         注意:ByteCharacterShort都作为Integer类型

3)除法

l         除法操作“/”和“/=”在操作数中有FloatDouble类型时,结果为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
    
    
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值