5

I have the following function:

% Formats inputed number with 2 digits after the decimal place
\newcommand*{\formatNumber}[1]{\FPround{\cost}{#1}{2}\cost} %

\cost goes through some calculations before this.

Is it possible to change this so that it rounds to 2 digits after the decimal place and uses a comma as separator?

  • Welcome to TeX.SX! Do you know about the possibilities with expl3? Can you show something more about these “calculations” before the usage of \cost? – egreg Jan 19 '15 at 7:52
6

expl3 provides a host of formatting options via siunitx. The following should suit your needs:

\documentclass{article}
\usepackage{siunitx}
\ExplSyntaxOn
  \cs_new_eq:NN \calc \fp_eval:n
\ExplSyntaxOff

\newcommand*{\formatNumber}[2][]{\num[%
  round-mode=places,% Round output to specified number of places
  round-precision=3,% Round-precision is 3
  output-decimal-marker={,},% Use , as decimal marker
  #1% Other options
  ]{\calc{#2}}}

\begin{document}

\formatNumber{2*3/5+pi}% 4.342

\formatNumber[round-precision=1]{2*3/5+pi}% 4.3

\newcommand{\cost}{20}

\formatNumber{\cost+pi}% 23.142

\end{document}

4,342
4,3
23,142

Options can be set globally (via \sisetup{<options>}) or locally, as is done inside the options for \num[<options>].

5

I suggest you load the siunitx package and create a dedicated macro, using the macro \num that's provided by the siunitx package, to achieve your formatting objectives. Note that rounding to two digits is performed automatically, as well as setting , (a comma) as the output decimal marker. As inputs, both , or . are OK as the decimal markers.

enter image description here

\documentclass{article}
\usepackage{siunitx}
\newcommand*{\mynum}[1]{\num[output-decimal-marker={,},
                             round-mode=places,
                             round-precision=2,
                             group-digits=false]{#1}}
\newtheorem{theorem}{Theorem}
\begin{document}
\mynum{12345.6789}

\newcommand{\cost}{9876,5432}  % just for this example
\mynum{\cost}
\end{document}
3

Another option is the numprint package and its command \nprounddigits{}, which is switched off by \npnoround.

Default language of numprint is German, and so are its default typographical conventions. You can switch to the language specified in option of the babel package by using the autolanguage option. If babel is not loaded, the autolanguage option makes numprint opt for the English conventions.

\documentclass{article}
\usepackage[np, autolanguage]{numprint}
\begin{document}
  \nprounddigits{2}
  \np{2.192}\quad\np{2.199}\quad\np{1.1853}\par
  \npnoround
  \np{1.1853}
\end{document}

enter image description here

Otherwise, if you have inserted, for example, \usepackage[french]{babel} in the preamble of your document, the autolanguage option of numprint will make your document follow the French typographical conventions:

enter image description here

Up to now numprint supports German, English, French, Spanish, Portuguese and Dutch typographical conventions for numbers.

  • How would you enable the use of , as the output decimal marker? – Mico Jan 19 '15 at 8:51
  • @Mico By using the \npdecimalsign{} command, with you decimal marker as argument. The recommended way is to use the conventions of your document's language, however. – Franck Pastor Jan 19 '15 at 9:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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