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

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

  \cs_new_eq:NN \calc \fp_eval:n

  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


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

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


\formatNumber{\cost+pi}% 23.142



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

| improve this answer | |

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


\newcommand{\cost}{9876,5432}  % just for this example
| improve this answer | |

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.

\usepackage[np, autolanguage]{numprint}

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.

| improve this answer | |
  • 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.