ClojureDocs

Nav

Namespaces

num

clojure.core

Available since 1.0 (source)
  • (num x)
Coerce to Number
1 Example
user=> (num 2048)
2048


;; Calling a Number http://download.oracle.com/javase/6/docs/api/ method:

user=> (def x (num 2048))
#'user/x

user=> (.floatValue x)
2048.0
See Also

Returns true if x is a Number

2 Notes
    By , created 5.8 years ago

    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.

    By , created 5.8 years ago, updated 5.8 years ago

    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")