Coerce to Number
num
is used to coerce a primitive Java number type such as int, float, long, double, etc., into its boxed version such as Float, Long, Double, etc. If given an existing boxed Number type, as opposed to a primitive number type, it will just return it as is.
If you are trying to parse a String into a Number, this is not the function you are looking for. num
only coerces from other primitive or boxed number types. For coercing from a String, you want to use Java interop such as:
(Long/parseLong "333") (Float/parseFloat "333.33") (Double/parseDouble "333.3333333333332") (Integer/parseInt "-333") (Integer/parseUnsignedInt "333") (BigInteger. "3333333333333333333333333332") (BigDecimal. "3.3333333333333333333333333332") (Short/parseShort "400") (Byte/parseByte "120")
You can also alternatively, if you want to parse the String into a Number the same way that the Clojure reader does so, use the edn reader.
(require '[clojure.edn :as edn]) (edn/read-string "333")