How to round numbers to hundreds, i.e., to the nearest multiple of 100
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}