syntax | since v0.0-1853 | in clojure | in edn | Edit |
ClojureScript numbers are the same as JavaScript numbers (floats). Available formats:
123
1.23
12e3
1.2e3
1.2e-3
0x123
0123
2r0110
NrXXX
where (<= 2 N 36)
and X
is in [0-9,A-Z]
Numbers are double-precision 64-bit format IEEE 754.
Decimal points are not allowed when specifying a custom base.
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
(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)))))