emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [O] rounding (up)


From: John Kitchin
Subject: Re: [O] rounding (up)
Date: Thu, 22 Jun 2017 08:02:49 -0400
User-agent: mu4e 0.9.19; emacs 25.2.1

It appears Emacs uses Banker's rounding (http://wiki.c2.com/?BankersRounding):

#+BEGIN_SRC emacs-lisp
(list (round 4.5) (round 5.5))
#+END_SRC

#+RESULTS:
| 4 | 6 |

Here is some lightly tested code to get different styles of rounding:

#+BEGIN_SRC emacs-lisp
(defun custom-round (number &optional N direction)
  "Round NUMBER to N decimal places.
DIRECTION is a symbol of how to round.
`round' does Banker's rounding.
`ceiling' rounds up.
`floor' rounds down.
`truncate' rounds towards zero.
https://en.wikipedia.org/wiki/Rounding";
  (setq N (or N 0)
        direction (or direction 'ceiling))
  (let ((m (float (expt 10 (* -1 N)))))
    (* (funcall direction (/ number m)) m)))

(list (custom-round 0.4875 2 'ceiling) (custom-round 0.975 2 'ceiling))
#+END_SRC

#+RESULTS:
| 0.49 | 0.98 |

#+BEGIN_SRC emacs-lisp
(list (custom-round 0.4875 2 'floor) (custom-round 0.975 2 'floor))
#+END_SRC

#+RESULTS:
| 0.48 | 0.97 |


Uwe Brauer writes:

> Hi
>
> It seems that org-table (and the underlying calc implementation) round
> down not up.
>
>
> Please consider
>
> | 3.25 | 0.4875 |
> |  6.5 |  0.975 |
> #+TBLFM: $2=$1*0.15;
>
>
> | 3.25 | 0.49 |
> |  6.5 | 0.97 |
> #+TBLFM: $2=$1*0.15;%.2f
>
> Is there a way to obtain
>
> | 3.25 | 0.49 |
> |  6.5 | 0.98 |
> #+TBLFM: $2=$1*0.15;%.2f
>
> That is to replace rounding down to rounding up?
>
> Thanks
>
> Uwe Brauer


--
Professor John Kitchin
Doherty Hall A207F
Department of Chemical Engineering
Carnegie Mellon University
Pittsburgh, PA 15213
412-268-7803
@johnkitchin
http://kitchingroup.cheme.cmu.edu



reply via email to

[Prev in Thread] Current Thread [Next in Thread]