改變 Spreadtab 中的小數位數

改變 Spreadtab 中的小數位數

我有一個預算表,它使用 Spreadtab。

\STautoround*{2}
\begin{spreadtab}{{tabular}{lrrrr}}
@\textbf{Item}                  &@\textbf{Unit Value}&@\textbf{Unit Cost}&@\textbf{Quantity}&@\textbf{Total Price}\\
\hline
@7-segment display       &10        &1.00     &1050    &[-1,0] * [-2,0]\\
@Capacitor (100nF)       &0.08      &0.08     &1400    &[-1,0] * [-2,0]\\
@Capacitor (10$\mu$F)    &0.10      &0.10     &350     &[-1,0] * [-2,0]\\
\end{spreadtab}

我想將價格四捨五入到小數點後兩位,並將數量四捨五入到零。目前,此程式碼將它們全部四捨五入到小數點後兩位。如何選擇每列的小數位數?

答案1

您可以使用例如包numprint,它使您可以使用列類型N{6}{2}來格式化列中的數字,並且\nprounddigits{2}您可以根據需要對數字進行舍入。

您將程式碼片段變更為 MWE:

\documentclass[ngerman]{scrartcl}

\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage{babel}

\usepackage{spreadtab}
\usepackage{numprint}


\begin{document}

\STsetdecimalsep{,}  % German writing: comment it for english
\nprounddigits{2}

\begin{spreadtab}{{tabular}{lrrrN{6}{2}}}
@\textbf{Item}           &@\textbf{Unit Value}
                                    &@\textbf{Unit Cost}
                                              &@\textbf{Quantity}
                                                       &@\textbf{Total Price}\\
\hline
@7-segment display       &10        &1.13     &1050    &[-1,0] * [-2,0]\\
@Capacitor (100nF)       &0.08      &0.08     &1400    &[-1,0] * [-2,0]\\
@Capacitor (10$\mu$F)    &0.10      &0.10     &350     &[-1,0] * [-2,0]\\
\end{spreadtab}

\end{document}

你會得到結果:

在此輸入影像描述

答案2

我建議使用功能強大的siunitx軟體包。

\documentclass{scrartcl}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}

\usepackage{spreadtab}
\usepackage{siunitx}
\usepackage{booktabs}

\newcolumntype{E}[1]{% E for Euro; the argument is the number of digits in the mantissa
  S[table-format=#1.2,round-integer-to-decimal,round-mode=places,round-precision=2]%
}

\begin{document}

\begin{spreadtab}{%
  {tabular}{
    l
    S[table-format=2.2]
    E{1}
    S[table-format=4.0]
    S[table-format=4.2,round-mode=places,round-integer-to-decimal,round-precision=2]% customize at will
  }%
}
\toprule
@\textbf{Item}           &@\textbf{Unit Value}
                                    &@\textbf{Unit Cost}
                                              &@\textbf{Quantity}
                                                       &@\textbf{Total Price}\\
\midrule
@7-segment display       &10        &1.13     &1050    &[-1,0] * [-2,0]\\
@Capacitor (\SI{100}{\nano\farad})
                         &0.08      &0.08     &1400    &[-1,0] * [-2,0]\\
@Capacitor (\SI{10}{\micro\farad})
                         &0.10      &0.10     &350     &[-1,0] * [-2,0]\\
\bottomrule
\end{spreadtab}

\end{document}

在此輸入影像描述

相關內容