如何在org-mode中将表格字段格式化为货币形式

4

我想在org-mode表格中将字段格式设置为货币格式,即使用货币符号($)和千位分隔符逗号。 我一直在使用$ %.2f来获取例如$ 1000.00,但如何获得逗号分隔符,例如$ 1,000.00? 我已经仔细阅读了手册,但可能我太愚蠢无法理解。 calc或elisp公式都可以。 请参见下面的示例表:

| Item     | Quantity |  Price |      Ext |
|----------+----------+--------+----------|
| Widget 1 |       10 | 100.00 |  1000.00 |
| Widget 2 |        5 |  50.00 |   250.00 |
| Widget 3 |        1 |   5.00 |     5.00 |
|----------+----------+--------+----------|
|          |          |  Total | $1255.00 |
#+TBLFM: $4=($2*$3);%.2f::@5$4=vsum(@2..@4);$%.2f
1个回答

2
我发现没有一种方法可以始终如一地进行操作,使得你可以获得有千位分隔符的数字,并且这些数字能够被正确地解释用于进一步的计算。因此,这不是一个答案,只是记录了我的研究成果。
以下示例steals code使用代码格式化具有千位分隔符的数字。在代码上按C-c C-c定义该函数,或添加到您的初始化文件中。
然后,使用elisp计算总金额,并使用新的格式化函数进行转换。
#+begin_src elisp :results none
(defun my-thousands-separate (num)
  "Formats the (possibly floating point) number with a thousands
separator."
  (let* ((nstr (number-to-string num))
         (dot-ind (string-match "\\." nstr))
         (nstr-no-decimal (if dot-ind
                               (substring nstr 0 dot-ind)
                             nstr))
         (nrest (if dot-ind
                    (substring nstr dot-ind)
                  nil))
         (pretty nil)
         (cnt 0))
    (dolist (c (reverse (append nstr-no-decimal nil)))
      (if (and (zerop (% cnt 3)) (> cnt 0))
          (setq pretty (cons ?, pretty)))
      (setq pretty (cons c pretty))
      (setq cnt (1+ cnt)))
    (concat pretty nrest)))
#+end_src

| Item     | Quantity |      Price |          Ext |
|----------+----------+------------+--------------|
| Widget 1 | 10       | 1001001.00 |  10010010.00 |
| Widget 2 | 5        |  501001.00 |   2505005.00 |
| Widget 3 | 1        |   51001.00 |     51001.00 |
|----------+----------+------------+--------------|
|          |          |      Total | 12,566,016.0 |
#+TBLFM: $4=($2*$3);%.2f::@5$4='(my-thousands-separate (apply '+ '(@2..@4)));N

请注意,如果您对行总计执行相同的操作,则逗号分隔的数字将无法正确解释为总计。
正确的方法应该是设置数字区域设置并让printf完成工作,但我不知道如何在emacs中进行设置。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接