How to display numbers in different bases under elisp?

为君一笑 提交于 2019-12-01 15:02:34

问题


As we know, elisp supports number in different bases, e.g. #20r1j equals to 39 in base-10. I want to display #20r1j as #20r1j. But (format "%d" #20r1j) gives me 39. How to keep the number in its original base?


回答1:


As a format string, you are quite limited in the bases you can display:

%d means print as number in decimal (%o octal, %x hex).
%X is like %x, but uses upper case.

You can use the calc library to manage this for you, however:

(require 'calc-bin)

(let ((calc-number-radix 20))
  (math-format-radix 39))
"1J"

(let ((calc-number-radix 20))
  (math-format-radix #20r1j))
"1J"

As with the read syntax you're using, allowed values of calc-number-radix run from 2 to 36.



来源:https://stackoverflow.com/questions/10360196/how-to-display-numbers-in-different-bases-under-elisp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!