Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I need something that corresponds to floor() to use it in a macro (I don't mean the notation, I really mean the function that rounds downwards).

Something like this:

\floor{34.75} produces 34

Anyone an idea?

share|improve this question

5 Answers 5

up vote 14 down vote accepted

Use the floating point module of expl3; it also accepts expressions in a quite natural form, even with scientific format.

\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\DeclareExpandableDocumentCommand{\floor}{m}
 {
  \fp_eval:n { floor ( #1 ) }
 }
\ExplSyntaxOff

\begin{document}

$\floor{34.75}$

$\floor{-1.25}$

$\floor{2}$

$\floor{-1}$

$\floor{.3}$

$\floor{-.2}$

$\floor{7/3}$

$\floor{1e2/31}$

$\floor{-1000/333}$

$\floor{1000/333-1/332}$

$\floor{5*12/(2+9)}$

\end{document}

Note that this is fully expandable, so that it can even go in an \edef. I used $...$ just to avoid problems with the minus sign.

enter image description here

share|improve this answer

You can also use the pgf math. As this is pgf all sorts of mathematical expressions can be evaluated:

enter image description here

Notes:

  • There is a discrepancy between floor{-1} between pgf and the other answers here, which I think is a bug?

Code:

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}

\newcommand{\Floor}[1]{%
    \pgfmathparse{int(floor(#1))}%
    \text{Floor} (#1)=\pgfmathresult%
}%

\begin{document}


$\Floor{34.75}$

$\Floor{-1.25}$

$\Floor{2}$

$\Floor{-1}$

$\Floor{.3}$

$\Floor{-.2}$

$\Floor{7/3}$

$\Floor{1e2/31}$

$\Floor{-1000/333}$

$\Floor{1000/333-1/332}$

$\Floor{5*12/(2+9)}$

\medskip
$\Floor{sin(90)}$

$\Floor{sin(-75)}$


$\Floor{cos(90)}$

$\Floor{cos(200)}$

\end{document}
share|improve this answer
    
Ah, you beat me! I'll delete my answer. Perhaps you could consider using int to suppress the decimal part? –  Gonzalo Medina May 5 at 23:35
    
@GonzaloMedina: Good point. Have updated solution. –  Peter Grill May 5 at 23:39
1  
The main difference is that the expl3 macro is fully expandable. –  egreg May 5 at 23:42
2  
floor(-1)=-2 is surely a bug. –  egreg May 6 at 8:02

Late to the party, here's a Lua-based approach. Probably overkill, but hey, it's fun. :)

\documentclass{article}

\usepackage{luacode}

\newcommand\floor[1]{\luadirect{tex.print(math.floor(#1))}}

\begin{document}

$\floor{34.75}$

$\floor{-1.25}$

$\floor{2}$

$\floor{-1}$

$\floor{.3}$

$\floor{-.2}$

$\floor{7/3}$

$\floor{1e2/31}$

$\floor{-1000/333}$

$\floor{1000/333-1/332}$

$\floor{5*12/(2+9)}$

\end{document}

Code borrowed from egreg :)

share|improve this answer
    
I was also late to the party, but these guys are tough and the modest additions (7/3, 1e2/31, -1000/33 and also 1000/333-1/332) I had made to the provided samples immediately got recycled in the other earlier answers... ;-) –  jfbu May 6 at 12:11
    
@jfbu: ooh! :) –  Paulo Cereda May 6 at 12:23

Without any packages...

The Logic:

\floor sets it up for parsing and calls on...

\floorhelper determines if it contains a number that follows a decimal. If not, just print it. If so, call on...

\floorhelphelper determines if number that follows decimal is bigger than "0". If not, print the base number. If so, call on...

\floorhelphelphelper If the base number is negative, round it down; if positive, truncate it.

\documentclass{article}
\newcounter{myfloor}
\def\floor#1{\floorhelper#1.\relax}
\def\floorhelper#1.#2\relax{\ifx\relax#2\relax#1\else\if.#2#1\else\floorhelphelper{#1}#2\fi\fi}
\def\floorhelphelper#1#2.{\ifnum#2>0\floorhelphelphelper#1\relax\relax\relax\else#1\fi}
\def\floorhelphelphelper#1#2\relax{%
  \if-#1\relax-\setcounter{myfloor}{0#2}\stepcounter{myfloor}\themyfloor%
  \else\setcounter{myfloor}{0#1#2}\themyfloor\fi}
\begin{document}

$\floor{34.75}$

$\floor{-1.25}$

$\floor{2}$

$\floor{-1}$

$\floor{.3}$

$\floor{-.2}$

$\floor{-2.000}$

$\floor{7.0}$

$\floor{8.}$

$\floor{-5.}$

\end{document}

enter image description here

share|improve this answer

Use \xintFloor command from the xintfrac package.

  1. It is completely expandable, hence can even go in an \edef or other contexts needing expandability.

  2. It natively accepts fractions such as 1000/333 as input, and scientific notation such as 1.234e2; if you need even more general input involving infix operations, there is the floor function provided by package xintexpr.

  3. Notice furthermore that the size of the inputs is not limited, thus for example

    \xintFloor {100000000000000000000000/100000000000000000000001}

    does provide the correct value 0 and not something such as 1 due to truncation of the numerator and denominator to approximated values with less precision. You may also consider:

    \xinttheexpr floor (100+3828/9007+3752/9005-3376/9003-4201/9002)\relax

    which correctly finds 99, not 100. Or

    \xinttheexpr floor(1+600001/900002+450002/900003-150001/900005)\relax

    which correctly finds 1 and not 2.

Examples

\documentclass{article}
\usepackage{xintfrac}

\begin{document}

$\xintFloor{34.75}$,
$\xintFloor{-1.25}$,
$\xintFloor{2}$,
$\xintFloor{-1}$,
$\xintFloor{.3}$,
$\xintFloor{-.2}$

and also
$\xintFloor{7/3}$,
$\xintFloor{1e2/31}$,
$\xintFloor{-1000/333}$, and
$\xintFloor {100000000000000000000000/100000000000000000000001}$.

\end{document}

And with \usepackage{xintexpr}, one can do things such as:

\xinttheexpr floor(1000/333-1/332)\relax 

Related: \xintCeil or the ceil function in \xintexpr-essions.

Also related: \xintTFrac and the frac function, they extract the fractional part.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.