Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

How to do this...

share|improve this question
add comment

1 Answer

up vote 28 down vote accepted

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}
share|improve this answer
    
Thanks to Jake and Peter Grill... –  sandu Jun 8 '12 at 6:50
    
@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 Jun 9 '12 at 10:07
2  
@dgs: Have added the requested info. If it can be improved in any way, please let me know. –  Peter Grill Jun 10 '12 at 7:29
1  
@PeterGrill Nicely done, thanks. –  dgs Jun 10 '12 at 14:34
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.