What's the easiest way to parse numbers in clojure?

后端 未结 10 1543
长发绾君心
长发绾君心 2020-12-08 09:30

I\'ve been using java to parse numbers, e.g.

(. Integer parseInt  numberString)

Is there a more clojuriffic way that would handle both int

10条回答
  •  一生所求
    2020-12-08 09:51

    Brian Carper's suggested approach (using read-string) works nicely, but only until you try and parse zero-padded numbers like "010". Observe:

    user=> (read-string "010")
    8
    user=> (read-string "090")
    java.lang.RuntimeException: java.lang.NumberFormatException: Invalid number: 090 (NO_SOURCE_FILE:0)
    

    This is because clojure tries to parse "090" as an octal, and 090 is not a valid octal!

提交回复
热议问题