我有下表。我需要对其进行格式化,以便显示的数字四舍五入到小数点后两位。我搜索了一下,发现所有解决方案都涉及使用 Latex 包。我不能使用包(我想是吧?)因为我正在使用 ShareLatex。
\documentclass{article}
\usepackage{booktabs}
\usepackage{tabularx}
\begin{document}
\begin{table}[tbp] \centering%
\newcolumntype{C}{>{\centering\arraybackslash}X}
\caption{Differences}%
{\normalsize
\begin{tabularx}{\textwidth}{lCCCC}
\toprule
Economy&year&nef\_en&gdi\_wer&inf\\\tabularnewline
\midrule\addlinespace[1.5ex]
Belize&2006&4.240267&-7.113768&-11.35403 \tabularnewline
Central African Republic&2014&8.2&25.28281&17.08281 \tabularnewline
Iran, Islamic Rep.&2012&30.5&27.35739&-3.142611 \tabularnewline
Iran, Islamic Rep.&2013&34.78927&39.26636&4.477088 \tabularnewline
Iceland&2009&16.27129&12.00581&-4.265474 \tabularnewline
Kenya&2006&6&14.45373&8.453734 \tabularnewline
Kenya&2007&4.3&9.758881&5.45888 \tabularnewline
Kenya&2008&16.2&26.23982&10.03982 \tabularnewline
Kuwait&2008&6.3&10.58271&4.28271 \tabularnewline
Myanmar&2013&8.876498&5.524279&-3.352219 \tabularnewline
Mongolia&2007&17.70245&9.045246&-8.657202 \tabularnewline
Mongolia&2013&12.5&8.604828&-3.895172 \tabularnewline
Mongolia&2015&1.9&5.775949&3.87595 \tabularnewline
Chad&2011&2.040816&-3.704296&-5.745112 \tabularnewline
Chad&2012&7.512763&14.01821&6.505443 \tabularnewline
\bottomrule \addlinespace[1.5ex]
\end{tabularx}%
}
\end{table}%
\end{document}
答案1
ShareLaTeX 应该与完整发行版一起安装。以下工作和用途siunitx
将第 3 至第 5 列四舍五入为小数点后两位(的默认值round-precision
):
\documentclass{article}
\usepackage{booktabs}
\usepackage{tabularx,siunitx}
\begin{document}
\begin{table}
\centering
\caption{Differences}
\begin{tabularx}{\textwidth}{ X c *{3}{S[round-mode = places]} }
\toprule
Economy & year & {nef\_en} & {gdi\_wer} & {inf} \\
\midrule
\addlinespace[1.5ex]
Belize & 2006 & 4.240267 & -7.113768 & -11.35403 \\
Central African Republic & 2014 & 8.2 & 25.28281 & 17.08281 \\
Iran, Islamic Rep. & 2012 & 30.5 & 27.35739 & -3.142611 \\
Iran, Islamic Rep. & 2013 & 34.78927 & 39.26636 & 4.477088 \\
Iceland & 2009 & 16.27129 & 12.00581 & -4.265474 \\
Kenya & 2006 & 6 & 14.45373 & 8.453734 \\
Kenya & 2007 & 4.3 & 9.758881 & 5.45888 \\
Kenya & 2008 & 16.2 & 26.23982 & 10.03982 \\
Kuwait & 2008 & 6.3 & 10.58271 & 4.28271 \\
Myanmar & 2013 & 8.876498 & 5.524279 & -3.352219 \\
Mongolia & 2007 & 17.70245 & 9.045246 & -8.657202 \\
Mongolia & 2013 & 12.5 & 8.604828 & -3.895172 \\
Mongolia & 2015 & 1.9 & 5.775949 & 3.87595 \\
Chad & 2011 & 2.040816 & -3.704296 & -5.745112 \\
Chad & 2012 & 7.512763 & 14.01821 & 6.505443 \\
\bottomrule
\end{tabularx}
\end{table}
\end{document}