36

I include the number as \num{0.12368455}. I need to print '0.1237'.

How to do this...

0

1 Answer 1

73

With the the siunitx package you can use the option round-mode=places, round-precision=4 to obtain the desired results.

  1. If you only need to control the rounding for a few numbers you use it as an option to \num:

    enter image description here

    \documentclass{article}
    \usepackage{siunitx}
    \begin{document}
    \num{0.12368455}
    
    \num[round-mode=places,round-precision=4]{0.12368455}
    \end{document}
    
  2. If you need to do this for the entire document you can use \sisetup{round-mode=places,round-precision=4} so that this formatting is applied for the entire document:

    enter image description here

    \documentclass{article}
    \usepackage{siunitx}
    \sisetup{round-mode=places,round-precision=4}
    
    \begin{document}
       \num{0.12368455}
    \end{document}
    
  3. Another way of achieving this is to use round-mode=figures, round-precision=4.

    enter image description here

    \documentclass{article}
    \usepackage{siunitx}
    
    \begin{document}
        \num[round-mode=figures,round-precision=4]{0.12368455}\par
        \num[round-mode=figures,round-precision=4]{0.12}\par
        \num[round-mode=places,round-precision=4]{0.12368455}\par
        \num[round-mode=places,round-precision=4]{0.12}
    \end{document}
    

round-mode=figures vs round-mode=places

With [round-mode=figures,round-precision=4] you get 4 significant figures, and with [round-mode=places,round-precision=4] you get 4 decimal places. Here is a comparison:

enter image description here


Similar results can also be achieved using \pgfmathprintnumber:

enter image description here

Code:

\documentclass{article}
\usepackage{pgf}

\newcommand*{\MyNum}[1]{%
    \pgfmathprintnumber[
        fixed,
        precision=4,
        fixed zerofill=true,
        ]{#1}}%

\begin{document}
\MyNum{0.12368455}\par
\MyNum{0.12}
\end{document}
4
  • @PeterGrill I'd suggest adding another example to the third paragraph to visualize the difference between figures and places, e.g. \num[...]{12.12}.
    – dgs
    Commented Jun 9, 2012 at 10:07
  • 2
    @dgs: Have added the requested info. If it can be improved in any way, please let me know. Commented Jun 10, 2012 at 7:29
  • Would you please tell me what option is responsible for printing 0.1234 as 0.123, while 0.12 should be printed as it is without adding an additional zero?
    – Diaa
    Commented Nov 4, 2019 at 22:59
  • 1
    @Diaa: I suggest you post a new question for that. If you are having difficulty chances are other will also so would make a good question. In the meantime you can use \newcommand*{\MyNum}[1]{\pgfmathprintnumber[precision=3]{#1}}. Commented Nov 4, 2019 at 23:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .