From mboxrd@z Thu Jan 1 00:00:00 1970 From: Karl Voit Subject: Re: Calc: Multiply time (hours) with a float Date: Sun, 18 Sep 2016 18:36:19 +0200 Message-ID: <2016-09-18T18-32-24@devnull.Karl-Voit.at> References: <2016-09-08T10-32-27@devnull.Karl-Voit.at> <2016-09-11T13-43-22@devnull.Karl-Voit.at> <87oa3u1vpn.fsf@luisa.c0t0d0s0.de> Reply-To: Karl Voit Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:40604) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1blf5D-0004eo-E1 for emacs-orgmode@gnu.org; Sun, 18 Sep 2016 12:36:48 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1blf5A-0005M6-8C for emacs-orgmode@gnu.org; Sun, 18 Sep 2016 12:36:47 -0400 Received: from [195.159.176.226] (port=49197 helo=blaine.gmane.org) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1blf5A-0005Jt-10 for emacs-orgmode@gnu.org; Sun, 18 Sep 2016 12:36:44 -0400 Received: from list by blaine.gmane.org with local (Exim 4.84_2) (envelope-from ) id 1blf4y-00042Q-HE for emacs-orgmode@gnu.org; Sun, 18 Sep 2016 18:36:32 +0200 List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: emacs-orgmode@gnu.org * Michael Welle wrote: > Hello, Hello Michael, > Karl Voit writes: > >> * Karl Voit wrote: >>> Hi! >>> >>> I do have following situation: I'd like to multiply a float with hours >>> which results in an error, obviously. However, I was not able to find >>> out how to do it. >>> >>> Minimal example with expected product: >>> >>> | time [h:m:s] | value | h * value | >>> |--------------+---------+-----------| >>> | 09:15:00 | 2.54321 | 23.524693 | >>> #+TBLFM: @2$3=$2*$1 >> > do you really need the result to be in decimal notation? If I want to implement my original idea (see my original posting): yes. > If not, > #+TBLFM:@2$3=$2*$1;T would work. If you need decimal notation you have > to provide your own function, I think. So I wrote some Elisp code: (defun org-time-string-to-seconds (s) "Convert a string HH:MM:SS to a number of seconds. Omitted third element will be interpreted as MM:SS with missing hours." ;; test with: ;; (message (concat "result is: " (number-to-string (org-time-string-to-seconds "57:45:03")))) ;; (message (concat "result is: " (number-to-string (org-time-string-to-seconds "57:45")))) (cond ((and (stringp s) (string-match "\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)" s)) (let ((hour (string-to-number (match-string 1 s))) (min (string-to-number (match-string 2 s))) (sec (string-to-number (match-string 3 s)))) (+ (* hour 3600) (* min 60) sec))) ((and (stringp s) (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)) (let ((min (string-to-number (match-string 1 s))) (sec (string-to-number (match-string 2 s)))) (+ (* min 60) sec))) ;;((stringp s) (string-to-number s)) ;;(t s) ) ) (defun org-time-string-to-hours (s) "Convert a string HH:MM:SS to hours (float). When only two values given, they will be interpreted as MM:SS with missing hours." ;; test via: ;; (message (concat "result is: " (number-to-string (org-time-string-to-hours "57:45:03")))) ;; (message (concat "result is: " (number-to-string (org-time-string-to-hours "57:45")))) (/ (org-time-string-to-seconds s) 3600.0) ) ... which is working: | time [h:m:s] | value | hours [float] | product | |--------------+---------+---------------+-----------| | 09:15:00 | 2.54321 | 9.25 | 23.524693 | #+TBLFM: $4=$2*$3::@2$3='(org-time-string-to-hours $1) However, when I try to calculate the product in one step, I get an error: | time [h:m:s] | value | product | |--------------+---------+---------| | 09:15:00 | 2.54321 | #ERROR | #+TBLFM: @2$3='(* $2 (org-time-string-to-hours $1)) When I replace "$2" in TBLFM with an integer value, it works. How can I calculate the product in /one/ step? Thanks again! -- mail|git|SVN|photos|postings|SMS|phonecalls|RSS|CSV|XML to Org-mode: > get Memacs from https://github.com/novoid/Memacs < https://github.com/novoid/extract_pdf_annotations_to_orgmode + more on github