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

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

    Use bigint and bigdec

    (bigint "1")
    (bigint "010") ; returns 10N as expected
    (bigint "111111111111111111111111111111111111111111111111111")
    (bigdec "11111.000000000000000000000000000000000000000000001")
    

    Clojure's bigint will use primitives when possible, while avoiding regexps, the problem with octal literals or the limited size of the other numeric types, causing (Integer. "10000000000") to fail.

    (This last thing happened to me and it was quite confusing: I wrapped it into a parse-int function, and afterwards just assumed that parse-int meant "parse a natural integer" not "parse a 32bit integer")

提交回复
热议问题