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

后端 未结 10 1528
长发绾君心
长发绾君心 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:52

    Brian carper's answer is almost correct. Instead of using read-string directly from clojure's core. Use clojure.edn/read-string. It is safe and it will parse anything that you throw at it.

    (ns edn-example.core
        (require [clojure.edn :as edn]))
    
    (edn/read-string "2.7"); float 2.7
    (edn/read-string "2"); int 2
    

    simple, easy and execution safe ;)

提交回复
热议问题