在 Clojure 中,将带前导 0 的数字传递给 str 会导致奇怪的行为。这是什么功能?

In Clojure, passing number with leading 0s to str causes strange behavior. What feature is this?

所以,我在玩一些字符串时无意中发现了这一点。

(str 111) => "111"
(str 0111) => "73"

这是什么?

前缀为0的号码是octal:

0111
=> 73

前缀为 0x 的号码是 hexadecimal:

0x111
=> 273

Xr 为前缀的数字,其中 X 是从 2 到 36 的数字,其基数为:

2r111
=> 7

如果您想用零填充数字,请参阅 format or cl-format:

(format "%04d" 111)
=> "0111"

(clojure.pprint/cl-format nil "~4,'0d" 111)
=> "0111"