將文件中的所有數字轉換為設定的小數位數

將文件中的所有數字轉換為設定的小數位數

我有一個表格,需要將所有數字自動格式化為兩位小數。這可能嗎?或者我需要格式化整個文件嗎?

編輯:

我已經應用了答案中建議的程式碼,但不幸的是它似乎不適用於我的特定表!有人對如何相應地調整它有任何想法嗎?

\documentclass{article}
\usepackage{siunitx}
\begin{document}

\sisetup{
table-number-alignment = center,
table-figures-integer = 1,
table-figures-decimal = 2
}

\begin{table}[thb]

\caption{\label{labelname}Table Caption}

\scriptsize
{\centering \begin{tabular
{lr@{\hspace{0cm}}c@{\hspace{0cm}}rr@{\hspace{0cm}}c@{\hspace{0cm}}r@{\hspace{0.1cm}}cr@{    space{0cm}}c@{\hspace{0cm}}r@{\hspace{0.1cm}}cr@{\hspace{0cm}}c@{\hspace{0cm}}r@{\hspace{0
1cm}}cr@{\hspace{0cm}}c@{\hspace{0cm}}r@{\hspace{0.1cm}}cr@{\hspace{0cm}}c@{\hspace{0cm}}r
{\hspace{0.1cm}}cr@{\hspace{0cm}}c@{\hspace{0cm}}r@{\hspace{0.1cm}}cr@{\hspace{0cm}}c@{\hs
ace{0cm}}r@{\hspace{0.1cm}}c}

\\
\hline
Dataset & \multicolumn{3}{c}{ConjunctiveRule }& \multicolumn{4}{c}{DecisionTable } &
\multicolumn{4}{c}{DTNB } & \multicolumn{4}{c}{JRip} & \multicolumn{4}{c}{NNge} &
\multicolumn{4}{c}{OneR} & \multicolumn{4}{c}{PART} & \multicolumn{4}{c}{Ridor} \\
\hline
Number correct & 9.5900 & $\pm$ & 1.16 & 9.5600 & $\pm$ & 1.22 &         & 9.5600 & $\pm$ &
1.22 &         & 9.5000 & $\pm$ & 1.24 &         & 8.9500 & $\pm$ & 1.34 &         & 9.6000
& $\pm$ & 1.16 &         & 9.3000 & $\pm$ & 1.34 &         & 9.1700 & $\pm$ & 1.42 &        \

\end{tabular} \scriptsize \par}
\end{table}
\end{document}

答案1

您必須將帶有十進制數字的每一列定義為S- 列。

表格中有 31 列,有些有小數,有些有數學符號,有些為空。在空的地方,我放了一個emdash(用於跟踪)。

這是一個 MWE(最小工作範例)。我使用scrartcl高 DIV 因子和橫向模式將表格擠壓在頁面邊緣之間。您可以透過使用geometry具有正確參數的 -package來實現相同的目的:

\documentclass[a4paper,landscape,DIV=20]{scrartcl}
\usepackage{siunitx}
\begin{document}
\sisetup{
  table-number-alignment = center,
  table-figures-integer = 1,
  table-figures-decimal = 2
}
\begin{table}[thb]
\caption{\label{labelname}Table Caption}
\centering\tiny
\begin{tabular}{
    l
    S[table-auto-round]
    c
    S[table-auto-round]
    S[table-auto-round]
    c
    S[table-auto-round]
    l
    S[table-auto-round]
    c
    S[table-auto-round]
    l
    S[table-auto-round]
    c
    S[table-auto-round]
    l
    S[table-auto-round]
    c
    S[table-auto-round]
    l
    S[table-auto-round]
    c
    S[table-auto-round]
    c
    S[table-auto-round]
    c
    S[table-auto-round]
    c
    S[table-auto-round]
    c
    S[table-auto-round]
    l}

Dataset & \multicolumn{3}{c}{ConjunctiveRule }& \multicolumn{4}{c}{DecisionTable } &
\multicolumn{4}{c}{DTNB } & \multicolumn{4}{c}{JRip} & \multicolumn{4}{c}{NNge} &
\multicolumn{4}{c}{OneR} & \multicolumn{4}{c}{PART} & \multicolumn{4}{c}{Ridor} \\

Number correct & 
9.5900 & 
$\pm$ & 
1.16 & 
9.5600 & 
$\pm$ &
1.22 &
--- &
9.5600 &
$\pm$ &
1.22 &
--- & 
9.5000 & 
$\pm$ & 
1.24 &
--- &
8.9500 & 
$\pm$ &
 1.34 &
--- & 
9.6000 & 
$\pm$ & 
1.16 & 
--- & 
9.3000 & 
$\pm$ 
& 1.34 & 
--- & 
9.1700 & 
$\pm$ &
 1.42 &
--- \\ 
\end{tabular}
\end{table}
\end{document}

在此輸入影像描述

答案2

請參閱第 57 頁siunitx文件.

一個小例子:

\documentclass{article}
\usepackage{siunitx}
\begin{document}
\sisetup{
  table-number-alignment = center,
  table-figures-integer = 1,
  table-figures-decimal = 2
}
\begin{tabular}{
    S[table-auto-round]
  }
  2.\\
  2.3\\
  2.34\\
  2.345
\end{tabular}
\end{document}

相關內容