如何將兩個長度相除並得到整數結果?

如何將兩個長度相除並得到整數結果?

我在除以兩個長度並得到整數結果時遇到問題。我試圖使用這個包:

\usepackage[nomessages]{fp}

這對我來說並沒有什麼用。我嘗試了多種方法,但仍然沒有成功。

你能幫我換個方法嗎?

答案1

您可以使用\numexprand \dimexpr,但這會舍入而不是截斷:

\documentclass{article}

\newcommand{\getlengthratio}[2]{%
  \number\numexpr
    \dimexpr#1\relax
     /
    \dimexpr#2\relax
  \relax}

\newcounter{test}

\begin{document}

There are \getlengthratio{\textwidth}{\parindent} parindents in a line

\setcounter{test}{\getlengthratio{\textwidth}{\parindent}}

The same number: \thetest

\end{document}

另一種方法是,您可以在地板、天花板和圓形之間進行選擇expl3

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\DeclareExpandableDocumentCommand{\getlengthratio}{mm}
 {
  \fp_eval:n { floor ( \dim_to_fp:n { #1 } / \dim_to_fp:n { #2 } , 0 ) }
 }
\ExplSyntaxOff

\newcounter{test}

\begin{document}

There are \getlengthratio{\textwidth}{\parindent} parindents in a line

\setcounter{test}{\getlengthratio{\textwidth}{\parindent}}

The same number: \thetest

\end{document}

在此輸入影像描述

相關內容