siunitx: How to remove the minus sign, if a number is rounded to zero?

siunitx: How to remove the minus sign, if a number is rounded to zero?

I want to use my raw data in my tables and let the siunitx package do the rounding, so that I can easily adjust the shown precision of my tables depending on my future needs. The problem is that on multiple occasions a -0.00001 or something similar will be rounded to -0.0. Although I understand that in some fields it might be good practice to show from which direction the numbers approach zero, it is quite uncommon in my field and I would like all numbers which are rounded to -0.0 to be displayed as 0.0 without the minus sign.

Is there a specific option to do this? I couldn't find anything in the siunitx documentation.

\documentclass{article}
\usepackage{siunitx}
\usepackage{booktabs, tabularx}

\begin{document}
  \sisetup{round-mode=places}
  \centering
  \begin{tabular}{
    *2{S[round-precision=1]}
  }\toprule
    {A} & {B}\\\midrule
    -0.0000001 & 2.5823\\
    -2.5823    & 0.0000001\\\bottomrule
  \end{tabular}
\end{document}

답변1

Indeed siunitx keeps the sign of the original number. As an alternative you can round using pgfplots, which removes the sign for fixed output (it keeps the sign in scientific output).

This can be combined with the collcell package to define a cell operation similar to the S column type from siunitx (see How to execute command on every table column). In the MWE below the an additional check is performed to see if the cell value is numeric (from the package xstring), to allow for table headers. Finally the headers are centered using a one-column multicolumn.

Edit: Note that the multicolumn (being type c) and the numeric check both prevent \pgfmathprintnumber from being called, only one of them would be sufficient for header rows.

MWE:

\documentclass{article}
\usepackage{collcell}
\usepackage{pgfplots}
\usepackage{xstring}

\newcommand{\roundsign}[1]{\IfDecimal{#1}{\pgfmathprintnumber[fixed,fixed zerofill,precision=1]{#1}}{#1}}
\newcolumntype{S}{>{\collectcell\roundsign}{r}<{\endcollectcell}}

\begin{document}
\begin{tabular}{S S}
\multicolumn{1}{c}{A} & \multicolumn{1}{c}{B}\\
\hline
-0.0000001 & 2.5823\\
\hline
-2.5823    & 0.0000001\\
\hline
\end{tabular}

\end{document}

Result:

enter image description here

답변2

You could use \fpeval to round the numbers and then siunitx to format them:

\documentclass{article}
\usepackage{siunitx,xfp}
\usepackage{booktabs, tabularx}

\begin{document}
  \sisetup{round-mode=places,round-integer-to-decimal}
  \centering
  \begin{tabular}{
    *2{S[round-precision=1]}
  }\toprule
    {A} & {B}\\\midrule
    \fpeval{round(-0.0000001,1)} & \fpeval{round(2.5823,1)}\\
    \fpeval{round(-2.5823,1)}    & \fpeval{round(0.0000001,1)}\\\bottomrule
  \end{tabular}
\end{document}

enter image description here

관련 정보