有沒有辦法用 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}

在此輸入影像描述

相關內容