
スプレッドタブを使用した予算表があります。
\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}
価格を小数点以下 2 桁に丸め、数量を小数点以下 0 桁にしたいと考えています。現在、このコードでは、すべてが小数点以下 2 桁に丸められています。列ごとに小数点以下の桁数を選択するにはどうすればよいですか?
答え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}