8

I would like to use LaTeX 3 to round up decimal numbers; I want something like the following: If I type

\RoundingUpFunction{2.7}

it should return

3

as output.

Unfortunately, I haven't taken the time to learn the LaTeX 3 syntax yet, so if someone will show me how to define such a function (or tell me what it is if it already exists), I will much appreciate it.

Update

If I try to use Marco Daniel's answer in the following way, it still doesn't work:

\documentclass{article}

\usepackage{pst-optic,pstricks-add}
\usepackage{xparse}

\newcommand*\Lens[1][]{%
  \psclip{%
    \psframe[linestyle=none](-\horizontal,-\vertical)(\horizontal,\vertical)
  }
  \rput(0,0){\lensSPH[#1]}
  \endpsclip
}

\ExplSyntaxOn
\NewDocumentCommand {\RoundUp} { m }
 {
  \fp_eval:n { round+(#1) }
 }
\ExplSyntaxOff

\begin{document}

\begin{figure}
\def\horizontal{1}
\def\vertical{1.7}
 \centering
  \begin{pspicture}[showgrid](-\RoundUp{\horizontal},-\RoundUp{\vertical})%
                             ( \RoundUp{\horizontal}, \RoundUp{\vertical})
   \Lens[
     lensType=CVG,
     lensWidth=1,
     lensColor=BleuVerre,
     drawing=false
   ]
  \end{pspicture}
\end{figure}

\end{document}

with the error message

! Undefined control sequence.
<argument> \LaTeX3 error: 
                           Unknown fp word a.
l.28 ...RoundUp{\horizontal}, \RoundUp{\vertical})

2 Answers 2

12

If you want to use LaTeX3 you can use ceil:

\documentclass{article}

\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand {\RoundingUpFunction} { m }
 {
  \fp_eval:n { ceil(#1) }
 }
\ExplSyntaxOff


\begin{document}
\RoundingUpFunction{1.7}%leads to 2

\RoundingUpFunction{1.2}%leads to 2

\RoundingUpFunction{1}%leads to 1

\end{document}

Related to your update use \DeclareExpandableDocumentCommand instead of \NewDocumentCommand

3
  • @SvendTveskæg: See update. Commented Jun 3, 2013 at 14:02
  • 1
    With the most recent LaTeX3, round+ should be ceil IIRC. Commented Jul 7, 2014 at 3:20
  • You forgot \usepackage{fp} in your preamble, but otherwise this example works for me. Thank you, I've been looking for a good amount of time for an answer. Commented May 13 at 18:19
8

If you can bear not to use LaTeX 3 then you could use TikZ instead...

\documentclass{article}
\usepackage{pgfmath,pgfkeys}

\newcommand\RoundingUpFunction[1]{%
  \pgfmathparse{ceil(#1)}%
  \pgfmathprintnumber[int trunc]{\pgfmathresult}%
}

\begin{document}
\RoundingUpFunction{2.7}
\end{document}

3

5
  • This is good relativt to what I first asked for. This doesn't work for me however, so I'll edit my question. Commented Jun 3, 2013 at 13:13
  • surely you meant "prepared not to use" rather than "happy not to use" :-) Commented Jun 3, 2013 at 13:25
  • @DavidCarlisle Fixed :-) Commented Jun 3, 2013 at 13:27
  • 3
    Minor detail: You only need pgfmath,pgfkeys (this is fixed so now, in CVS version, only pgfmath is enough). Also you can add int(ceil(#1)) so you can directly type \pgfmathresult
    – percusse
    Commented Jun 3, 2013 at 13:50
  • @percusse Thanks, I've fixed my answer. I thought I didn't need all of TikZ, but when I used just pgfmath it didn't work. I hadn't realised I'd need pgfkeys too. Commented Jun 3, 2013 at 14:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .