From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp1 ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms11 with LMTPS id OFPpDdmrhl9jBQAA0tVLHw (envelope-from ) for ; Wed, 14 Oct 2020 07:42:17 +0000 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp1 with LMTPS id uEKmCdmrhl9VagAAbx9fmQ (envelope-from ) for ; Wed, 14 Oct 2020 07:42:17 +0000 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id B6C969400C7 for ; Wed, 14 Oct 2020 07:42:16 +0000 (UTC) Received: from localhost ([::1]:54418 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kSbQN-0005P3-LU for larch@yhetil.org; Wed, 14 Oct 2020 03:42:15 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:53594) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kSbPN-0005MM-9H for emacs-orgmode@gnu.org; Wed, 14 Oct 2020 03:41:13 -0400 Received: from mailer-211-145.hitrost.net ([91.185.211.145]:22725) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kSbPK-0008VE-Mj for emacs-orgmode@gnu.org; Wed, 14 Oct 2020 03:41:12 -0400 Received: from [84.20.244.182] (helo=Tauriel) by b1.hitrost.net with esmtpsa (TLSv1.2:ECDHE-RSA-AES128-GCM-SHA256:128) (Exim 4.92) (envelope-from ) id 1kSbP5-007dAI-2o; Wed, 14 Oct 2020 09:40:55 +0200 References: <3444a52f-36a7-6e9d-46b9-272dddc7a3ef@grinta.net> <87wo0jttxz.fsf@christianmoe.com> <87sgaj26qq.fsf@christianmoe.com> <87sgajrd93.fsf@ucl.ac.uk> <87h7qy38k9.fsf@psi.ch> User-agent: mu4e 0.9.19; emacs 25.3.2 From: Christian Moe To: Derek Feichtinger Subject: Re: org-tables with monetary amounts In-reply-to: <87h7qy38k9.fsf@psi.ch> Date: Wed, 14 Oct 2020 09:38:08 +0200 Message-ID: <87blh51cm7.fsf@christianmoe.com> MIME-Version: 1.0 Content-Type: text/plain X-GeoIP: Country [IP], SI [84.20.244.182] X-Antivirus-Scanner: Clean mail though you should still use an Antivirus Received-SPF: pass client-ip=91.185.211.145; envelope-from=mail@christianmoe.com; helo=mailer-211-145.hitrost.net X-detected-operating-system: by eggs.gnu.org: First seen = 2020/10/14 03:41:02 X-ACL-Warn: Detected OS = Linux 3.11 and newer X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-orgmode@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Daniele Nicolodi , emacs-orgmode@gnu.org, Christian Moe , Eric S Fraga Errors-To: emacs-orgmode-bounces+larch=yhetil.org@gnu.org Sender: "Emacs-orgmode" X-Scanner: scn0 Authentication-Results: aspmx1.migadu.com; dkim=none; dmarc=none; spf=pass (aspmx1.migadu.com: domain of emacs-orgmode-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=emacs-orgmode-bounces@gnu.org X-Spam-Score: -1.01 X-TUID: mZ0iIwitf+j+ Hi, Derek, Very useful, thanks! Yours, Christian Derek Feichtinger writes: > Hi, > > On Mon, Oct 12 2020, Eric S Fraga wrote: > >> On Monday, 12 Oct 2020 at 10:22, Christian Moe wrote: >>> I think I was thinking about adding mode flags for unit computations, >>> like in the imagined example above (`$3=$2+$1;u'), similar to what has >>> been done for duration computations. >> >> This would be very useful, actually. I use embedded calc all the time >> with units; being able to use some of the same expressions in a table >> would be highly welcome. > > The functions that one can use in calc formulas can be extended using > the 'defmath' lisp command. I paste here from my notes (not about > monetary units, but easily adapted): > > +++++++++++++++++++++++++++++++ > Defining a new calc function for unit conversion with defmath > #+BEGIN_SRC emacs-lisp > (defmath uconv (expr target-units &optional pure) > (math-convert-units expr target-units pure)) > #+END_SRC > > #+RESULTS: > : calcFunc-uconv > > | km | ft | > |--------+--------------| > | 2.5 km | 8202.0997 ft | > #+TBLFM: $2=uconv($1, ft) > > Using the units from the table header (if the 3rd arg is given to > uconv, the output is stripped of the unit): > > | km | ft | > |-----+-----------| > | 2.5 | 8202.0997 | > #+TBLFM: $2 = uconv($1 * @<$1, @<$2, t) > > The standard calc function usimplify also works for this use > case: > > | km | ft | > |-----+-----------| > | 2.5 | 8202.0997 | > #+TBLFM: $2 = usimplify($1 * @<$1 / @<$2) > > A lisp equivalent of the above > #+begin_src elisp > (calc-eval "usimplify(2.5 km / ft)") > #+end_src > > #+RESULTS: > : 8202.09973753 > > Let's define a function that converts to base units > #+begin_src elisp > (defmath ustd (expr) (math-simplify-units (math-to-standard-units expr nil))) > #+end_src > > #+RESULTS: > : calcFunc-ustd > > | distance | time | speed | std unit speed | speed in ft/s | > |----------+--------+-------------+------------------+------------------| > | 3 km | 2.5 hr | 1.2 km / hr | 0.33333333 m / s | 1.0936133 ft / s | > #+TBLFM: @2$3=$1/$2::@2$4=ustd($3)::@2$5=uconv($-1, ft/s) > ++++++++++++++++++++++++++++++++++++