我使用javascript将一个String值发送给grails服务器,值为:“0.5”
$.post(myURL,{scale:"0.5"},fnPost);
当我将其投射如下:
Long scale= params?.scale as Long ;
我得到:
For input string: "0.5". Stacktrace follows:
java.lang.NumberFormatException: For input string: "0.5"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Long.parseLong(Long.java:419)
at java.lang.Long.valueOf(Long.java:525)
真的,在groovy看到“0.5”无法投入到Long中是很奇怪的!
答案 0 :(得分:2)
0.5是双倍的。您无法将其投射到Long
。您需要将其强制转换为Double
或BigDecimal
(默认为Groovy)
assert "0.5" as BigDecimal == 0.5G
assert "0.5" as Double == 0.5D
assert "5" as Long == 5L