# How to round numbers to hundreds, i.e., to the nearest multiple of 100

I want to round numbers to hundreds. I note Rounding a number to its hundred. However, the question asks about rounding 2,386.0 down to 2,300.0 -- and I would want it to be rounded up to 2,400.0, and I would want 2,326.0 to be rounded down to 2,300.0.

How to do this?

According to a comment by joseph-wright, it would be easy, but after reading and trying for an hour, I decided to ask here.

MWE

\documentclass{article}
\usepackage{xparse,siunitx}
\ExplSyntaxOn
\NewDocumentCommand{\hundreds}{O{}m}
{
\num[#1]{\fp_eval:n { trunc(#2,-2) }}
}
\ExplSyntaxOff
\newcommand{\myhundreds}[1]{\num[round-mode=figures,round-precision=-2]{#1}}
\begin{document}

\hundreds{2348}

\hundreds[group-four-digits,group-separator={,}]{2348}

\sisetup{group-four-digits,group-separator={,}}

\hundreds{2399}

\hundreds{2301}

\myhundreds{2300}
\end{document}


Use round rather than trunc. From the l3fp documentation:

\documentclass{article}
\usepackage{xparse,siunitx}

\ExplSyntaxOn
\NewDocumentCommand{\hundreds}{O{}m}
{
\num[#1]{\fp_eval:n { round(#2,-2) }}
}
\ExplSyntaxOff

\begin{document}

\hundreds{2348}

\hundreds[group-four-digits,group-separator={,}]{2348}

\sisetup{group-four-digits,group-separator={,}}

\hundreds{2399}

\hundreds{2301}

\end{document}


This prints 2300 2,300 2,400 2,300.

Just for the sake of variety, here's LuaLaTeX-based solution. It provides a LaTeX macro called \hundreds. The input of this macro must be either a number or an expression that evaluates to a number according to Lua's syntax rules; its output is that number rounded to the nearest multiple of 100. The LaTeX macro \hundreds uses LuaTeX's tex.sprint function as well as a utility Lua function called math.round_int which rounds a number to the nearest integer.

The \hundreds macro can be placed in the argument of \num for further pretty-printing.

\documentclass{article}
\usepackage[group-four-digits,group-separator={,}]{siunitx} % for \num macro
% define a Lua function called 'math.round_int':
\directlua{%
function math.round_int ( x )
return x>=0 and math.floor(x+0.5) or math.ceil(x-0.5)
end
}
\newcommand{\hundreds}[1]{\directlua{%
tex.sprint(100*math.floor(math.round_int((#1)/100)))}}

\begin{document}
\hundreds{2348};
\hundreds{2399};
\hundreds{2301};
\hundreds{23*100+10}; % arg evaluates to '2310'
\num{\hundreds{2301}}
\end{document}