number literal

syntaxsince v0.0-1853 in clojure in ednEdit

ClojureScript numbers are the same as JavaScript numbers (floats). Available formats:

  • decimal 123 1.23
  • exponent 12e3 1.2e3 1.2e-3
  • hexadecimal 0x123
  • octal 0123
  • binary 2r0110
  • arbitrary NrXXX where (<= 2 N 36) and X is in [0-9,A-Z]

Details:

Numbers are double-precision 64-bit format IEEE 754.

Decimal points are not allowed when specifying a custom base.


Examples:

123
;;=> 123

123.45
;;=> 123.45

Scientific notation;

12e3
;;=> 12000

1.2e-3
;;=> 0.0012

Standard hex and octal notations:

0x1f
;;=> 31

010
;;=> 8

Radix notation for using up to base 36.

2r10111
;;=> 23

8r32
;;=> 26

16rFF
;;=> 255

36rZ
;;=> 35

See Also:


Reader code @ tools.reader:src/main/clojure/clojure/tools/reader.clj
(defn- read-number
  [rdr initch]
  (loop [sb (doto (StringBuilder.) (.append initch))
         ch (read-char rdr)]
    (if (or (whitespace? ch) (macros ch) (nil? ch))
      (let [s (str sb)]
        (unread rdr ch)
        (or (match-number s)
            (err/throw-invalid-number rdr s)))
      (recur (doto sb (.append ch)) (read-char rdr)))))