emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* org-spreadsheet: formatting chops off units
@ 2012-09-05 21:46 Kyle Andrews
  2012-09-06  3:08 ` Nick Dokos
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Kyle Andrews @ 2012-09-05 21:46 UTC (permalink / raw)
  To: Emacs orgmode

Hello everyone,

I want to use an org-spreadsheet to perform unit conversions for me.

| Mass (g) | Mass (lb)     |
|----------+---------------|
| 300 g    | 0.66138679 lb |
| 23 kg    | 50.706320 lb  |
| 50 Mg    | 110231.13 lb  |
#+TBLFM: $2=uconvert($1, lb)


I made the table above, but can't figure out how to format the output. I
tried using the ;%.2f notation shown in the manual, but it cuts the
units off for some reason.

Here is what org-table displays with ;%.2f appended onto my table
formula:

| Mass (g) | Mass (lb)     |
|----------+---------------|
| 300 g    | 0.66          |
| 23 kg    | 50.71         |
| 50 Mg    | 110231.13     |
#+TBLFM: $2=uconvert($1, lb);%.2f


Here is what I wish appending ;%.2f would cause org-table to display:


| Mass (g) | Mass (lb)     |
|----------+---------------|
| 300 g    | 0.66 lb       |
| 23 kg    | 50.71 lb      |
| 50 Mg    | 110231.13 lb  |
#+TBLFM: $2=uconvert($1, lb);%.2f


If you type in '0.66138679 lb into calc directly, and press d f 2, calc
displays 0.66 lb as desired. Is there any reason for the %.2f notation
chops off the units? Is there some better way to accomplish what I
want?

Thanks!


Note: my spreadsheet uses the wrapper function below to convert units with calc.

#+begin_src emacs-lisp
(defmath uconvert (expression new-units)
    (math-convert-units expression new-units)))
#+end_src

--
Kyle C. Andrews
kyle.c.andrews@gmail.com

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: org-spreadsheet: formatting chops off units
  2012-09-05 21:46 org-spreadsheet: formatting chops off units Kyle Andrews
@ 2012-09-06  3:08 ` Nick Dokos
  2012-09-06  5:15 ` Michael Brand
  2012-09-07 17:28 ` Michael Brand
  2 siblings, 0 replies; 5+ messages in thread
From: Nick Dokos @ 2012-09-06  3:08 UTC (permalink / raw)
  To: Kyle Andrews; +Cc: Emacs orgmode

Kyle Andrews <kyle.c.andrews@gmail.com> wrote:

> Hello everyone,
> 
> I want to use an org-spreadsheet to perform unit conversions for me.
> 
> | Mass (g) | Mass (lb)     |
> |----------+---------------|
> | 300 g    | 0.66138679 lb |
> | 23 kg    | 50.706320 lb  |
> | 50 Mg    | 110231.13 lb  |
> #+TBLFM: $2=uconvert($1, lb)
> 
> 
> I made the table above, but can't figure out how to format the output. I
> tried using the ;%.2f notation shown in the manual, but it cuts the
> units off for some reason.
> 
> Here is what org-table displays with ;%.2f appended onto my table
> formula:
> 
> | Mass (g) | Mass (lb)     |
> |----------+---------------|
> | 300 g    | 0.66          |
> | 23 kg    | 50.71         |
> | 50 Mg    | 110231.13     |
> #+TBLFM: $2=uconvert($1, lb);%.2f
> 
> 
> Here is what I wish appending ;%.2f would cause org-table to display:
> 
> 
> | Mass (g) | Mass (lb)     |
> |----------+---------------|
> | 300 g    | 0.66 lb       |
> | 23 kg    | 50.71 lb      |
> | 50 Mg    | 110231.13 lb  |
> #+TBLFM: $2=uconvert($1, lb);%.2f
> 
> 
> If you type in '0.66138679 lb into calc directly, and press d f 2, calc
> displays 0.66 lb as desired. Is there any reason for the %.2f notation
> chops off the units? Is there some better way to accomplish what I
> want?
> 
> Thanks!
> 
> 
> Note: my spreadsheet uses the wrapper function below to convert units with calc.
> 
> #+begin_src emacs-lisp
> (defmath uconvert (expression new-units)
>     (math-convert-units expression new-units)))
> #+end_src
> 

There might be a better, more local, way of doing it, so take the following as an
existence proof: if you customize the variable org-calc-default-modes and change
the calc-float-format to (fix 2), the table will recalculate to your expectations.

I don't know how to stitch together the better solution however, so I leave that
as an exercise for you :-)

Nick

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: org-spreadsheet: formatting chops off units
  2012-09-05 21:46 org-spreadsheet: formatting chops off units Kyle Andrews
  2012-09-06  3:08 ` Nick Dokos
@ 2012-09-06  5:15 ` Michael Brand
  2012-09-07 17:28 ` Michael Brand
  2 siblings, 0 replies; 5+ messages in thread
From: Michael Brand @ 2012-09-06  5:15 UTC (permalink / raw)
  To: Kyle Andrews; +Cc: Emacs orgmode

Hi Kyle

On Wed, Sep 5, 2012 at 11:46 PM, Kyle Andrews <kyle.c.andrews@gmail.com> wrote:
> If you type in '0.66138679 lb into calc directly, and press d f 2, calc
> displays 0.66 lb as desired. Is there any reason for the %.2f notation
> chops off the units? Is there some better way to accomplish what I
> want?

Org spreadsheet gives access to the format of calc, use
#+TBLFM: $2=uconvert($1, lb);f-2
or less recommended the format of Org spreadsheet with a text
#+TBLFM: $2=uconvert($1, lb);%.2f lb

Michael

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: org-spreadsheet: formatting chops off units
  2012-09-05 21:46 org-spreadsheet: formatting chops off units Kyle Andrews
  2012-09-06  3:08 ` Nick Dokos
  2012-09-06  5:15 ` Michael Brand
@ 2012-09-07 17:28 ` Michael Brand
  2012-09-12  7:51   ` Bastien
  2 siblings, 1 reply; 5+ messages in thread
From: Michael Brand @ 2012-09-07 17:28 UTC (permalink / raw)
  To: Emacs orgmode

[-- Attachment #1: Type: text/plain, Size: 389 bytes --]

Hi all

On Wed, Sep 5, 2012 at 11:46 PM, Kyle Andrews <kyle.c.andrews@gmail.com> wrote:
> [...]
> #+TBLFM: $2=uconvert($1, lb)
> [...]
> #+begin_src emacs-lisp
> (defmath uconvert (expression new-units)
>     (math-convert-units expression new-units)))
> #+end_src

Calc `defmath' is not mentioned in the Org manual or on Worg, so I
suggest the attached patch for the Org manual.

Michael

[-- Attachment #2: 0001-org.texi-Mention-defmath.patch.txt --]
[-- Type: text/plain, Size: 1021 bytes --]

From 64c0fad8b4fddca4b8839fadc52d2f2e7086ebd6 Mon Sep 17 00:00:00 2001
From: Michael Brand <michael.ch.brand@gmail.com>
Date: Fri, 7 Sep 2012 19:24:14 +0200
Subject: [PATCH] org.texi: Mention Calc defmath

* org.texi (Formula syntax for Calc): Add a sentence to mention Calc
defmath.
---
 doc/org.texi |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)

diff --git a/doc/org.texi b/doc/org.texi
index b82d5db..c9fc008 100644
--- a/doc/org.texi
+++ b/doc/org.texi
@@ -2659,6 +2659,9 @@ if($1<20,teen,string(""))  @r{"teen" if age $1 less than 20, else empty}
 Note that you can also use two org-specific flags @code{T} and @code{t} for
 durations computations @ref{Durations and time values}.
 
+You can add your own Calc functions defined in Emacs Lisp with @code{defmath}
+and use them in formula syntax for Calc.
+
 @node Formula syntax for Lisp, Durations and time values, Formula syntax for Calc, The spreadsheet
 @subsection Emacs Lisp forms as formulas
 @cindex Lisp forms, as table formulas
-- 
1.7.4.2


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: org-spreadsheet: formatting chops off units
  2012-09-07 17:28 ` Michael Brand
@ 2012-09-12  7:51   ` Bastien
  0 siblings, 0 replies; 5+ messages in thread
From: Bastien @ 2012-09-12  7:51 UTC (permalink / raw)
  To: Michael Brand; +Cc: Emacs orgmode

Hi Michael,

Michael Brand <michael.ch.brand@gmail.com> writes:

> Calc `defmath' is not mentioned in the Org manual or on Worg, so I
> suggest the attached patch for the Org manual.

Applied, thanks!

-- 
 Bastien

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2012-09-12 11:30 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-09-05 21:46 org-spreadsheet: formatting chops off units Kyle Andrews
2012-09-06  3:08 ` Nick Dokos
2012-09-06  5:15 ` Michael Brand
2012-09-07 17:28 ` Michael Brand
2012-09-12  7:51   ` Bastien

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).