有没有办法用 Expl3 在四舍五入的数字末尾填充零?

有没有办法用 Expl3 在四舍五入的数字末尾填充零?

我已经能够使用 整理出一个三角函数值表expl3。但是,我想在末尾填充零,以便所有值都有四位小数。有没有直接的方法可以做到这一点,expl3还是我需要构建自己的宏来处理这个问题?

这与我刚刚发布的类似问题相关:有没有简单的方法可以提高三角函数 pgfmath 的精度?

这是我的 MWE:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{calc}

\def\mynum{0}
\def\myvoffset{0pt}

\usepackage{xparse}
\ExplSyntaxOn
\def\aetan#1{\fp_eval:n { round ( tand ( #1 ), 4 )}}
\def\aesin#1{\fp_eval:n { round ( sind ( #1 ), 4 )}}
\def\aecos#1{\fp_eval:n { round ( cosd ( #1 ), 4 )}}
\def\aemod#1{\fp_eval:n { #1 - round0 ( #1 / 5 , 0 ) * 5}}
\ExplSyntaxOff

\usepackage[margin=0.5in]{geometry}

\begin{document}

Using \LaTeX3

\begin{tikzpicture}

  \coordinate(UL) at (0,0);

  \node at (UL) {Degrees};
  \node[anchor=west] at ($(UL.west)+(1cm,0)$) {$\sin$};
  \node[anchor=west] at ($(UL.west)+(2.75cm,0)$) {$\cos$};
  \node[anchor=west] at ($(UL.west)+(4.50cm,0)$) {$\tan$};


  \foreach \myn in {1,2,3,...,45}
  {
    \ifnum\aemod{(\myn-1)}=0\relax
      \xdef\myvoffset{\dimexpr\myvoffset+1.350\baselineskip}%%
    \else
      \xdef\myvoffset{\dimexpr\myvoffset+1.00\baselineskip}%%
    \fi

    \coordinate (DEG/\myn)   at ($(UL.west)-(0,\myvoffset)$);
    \coordinate (DEG/S/\myn) at ($(DEG/\myn)+(1cm,0)$);
    \coordinate (DEG/C/\myn) at ($(DEG/S/\myn)+(1.75cm,0)$);
    \coordinate (DEG/T/\myn) at ($(DEG/C/\myn)+(1.75cm,0)$);

    \node[anchor=east] at (DEG/\myn)   {$\myn^\circ$};
    \node[anchor=west] at (DEG/S/\myn) {\texttt{\aesin{\myn}}};
    \node[anchor=west] at (DEG/C/\myn) {\texttt{\aecos{\myn}}};
    \node[anchor=west] at (DEG/T/\myn) {\texttt{\aetan{\myn}}};

  }

\end{tikzpicture}

\end{document}

在此处输入图片描述

答案1

我找到了一些解决方案,尽管它不是一个expl3解决方案。我使用siunitx包如下:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{siunitx}
\sisetup{add-decimal-zero,
         round-precision=4,
         round-mode=places,
         round-integer-to-decimal,
         detect-all}

\def\mynum{0}
\def\myvoffset{0pt}

\usepackage{xparse}
\ExplSyntaxOn
\def\aetan#1{\num{\fp_eval:n { round ( tand ( #1 ), 4 )}}}
\def\aesin#1{\num{\fp_eval:n { round ( sind ( #1 ), 4 )}}}
\def\aecos#1{\num{\fp_eval:n { round ( cosd ( #1 ), 4 )}}}
\def\aemod#1{\fp_eval:n { #1 - round0 ( #1 / 5 , 0 ) * 5}}
\ExplSyntaxOff

\usepackage[margin=0.5in]{geometry}

\begin{document}

Using \LaTeX3

\begin{tikzpicture}

  \coordinate(UL) at (0,0);

  \node at (UL) {Degrees};
  \node[anchor=west] at ($(UL.west)+(1cm,0)$) {$\sin$};
  \node[anchor=west] at ($(UL.west)+(2.75cm,0)$) {$\cos$};
  \node[anchor=west] at ($(UL.west)+(4.50cm,0)$) {$\tan$};


  \foreach \myn in {1,2,3,...,45}
  {

    \ifnum\aemod{(\myn-1)}=0\relax
      \xdef\myvoffset{\dimexpr\myvoffset+1.350\baselineskip}%%
    \else
      \xdef\myvoffset{\dimexpr\myvoffset+1.00\baselineskip}%%
    \fi

    \coordinate (DEG/\myn)   at ($(UL.west)-(0,\myvoffset)$);
    \coordinate (SIN/\myn) at ($(DEG/\myn)+(1cm,0)$);
    \coordinate (COS/\myn) at ($(SIN/\myn)+(1.75cm,0)$);
    \coordinate (TAN/\myn) at ($(COS/\myn)+(1.75cm,0)$);

    \node[anchor=east] at (DEG/\myn)   {$\myn^\circ$};
    \node[anchor=west] at (SIN/\myn) {\texttt{\aesin{\myn}}};
    \node[anchor=west] at (COS/\myn) {\texttt{\aecos{\myn}}};
    \node[anchor=west] at (TAN/\myn) {\texttt{\aetan{\myn}}};

  }

\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容