0

我用javascript向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)

真的,在常规中看到“0.5”不能转换为 Long 很奇怪!

4

1 回答 1

2

0.5 是双倍。您不能将其转换为Long. 您需要将其转换为Doubleor BigDecimal(Groovy 中的默认值)

assert "0.5" as BigDecimal == 0.5G
assert "0.5" as Double == 0.5D
assert "5" as Long == 5L
于 2013-10-23T17:08:57.520 回答