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.

Is there a way to format number strings while printing?

Suppose I have the following declaration:

\newcommand{\MyNumberA}{40}
\newcommand{\MyNumberB}{60}

The following in the text:

\the\numexpr(\MyNumberA*\MyNumberB)\relax

will produce 2400. I want to print 2,400 or 2,400.00.

Am I asking too much or this can be done? How?

share|improve this question
1  
another relevant package: numprint –  jfbu Apr 7 at 6:45

3 Answers 3

up vote 11 down vote accepted

You can use siunitx package to do this:

\documentclass{article}
\usepackage[group-separator={,},group-minimum-digits=4]{siunitx}
\begin{document}

\newcommand{\MyNumberA}{40}
\newcommand{\MyNumberB}{60}

\num{\the\numexpr(\MyNumberA*\MyNumberB)\relax}

\end{document}

which produces:

enter image description here

You can define your own command to handle this too:

\documentclass{article}
\usepackage{siunitx}
\newcommand\mynum[1]{\num[group-separator={,},group-minimum-digits=4]{\the\numexpr(#1)\relax}}
\begin{document}

\newcommand{\MyNumberA}{40}
\newcommand{\MyNumberB}{60}

\mynum{\MyNumberA*\MyNumberB}

\end{document}

Regarding comment about non-integer values:

I would follow @egreg's suggestion and take advantage of the fact that siunitx ueses expl3 internally. In this following example, I create a separate command to handle floating point values. I also provided the macro with two arguments. The first argument is optional to allow you to modify how siunitx handles its content:

\documentclass{article}
\usepackage{siunitx}
\ExplSyntaxOn
\DeclareExpandableDocumentCommand\myeval{m}{\fp_eval:n{#1}}
\ExplSyntaxOff
\newcommand\mynum[1]{\num[group-separator={,},group-minimum-digits=4]{\the\numexpr(#1)\relax}}
\newcommand\myfpnum[2][]{\num[group-separator={,},
                            group-minimum-digits=4,
                            round-integer-to-decimal,
                            round-mode=places,#1]{\myeval{#2}}}
\begin{document}

\def\MyNumberA{40}
\def\MyNumberB{60}

\mynum{\MyNumberA*\MyNumberB}

\def\MyNumberA{40.1349}
\def\MyNumberB{60.9982}

\myfpnum[round-precision=3]{\MyNumberA*\MyNumberB}

\end{document}

enter image description here

share|improve this answer
    
Works fine as long as the numbers are integers. If I change use a decimal fraction, it breaks. Any remedy there. –  deshmukh Apr 8 at 7:46
1  
@deshmukh I've updated my answer. Hopefully that helps. –  A.Ellett Apr 8 at 12:56
    
perfect. Thanks! –  deshmukh Apr 8 at 13:47

If you also want to print two zeroes after the number use this siunitx setup:

\sisetup{group-separator={,},group-minimum-digits=4,round-integer-to-decimal,round-mode=places}

MWE:

\documentclass{article}
\usepackage{siunitx}
\begin{document}

\newcommand{\MyNumberA}{40}
\newcommand{\MyNumberB}{60}

\sisetup{group-separator={,},group-minimum-digits=4,round-integer-to-decimal,round-mode=places}
\num{\the\numexpr(\MyNumberA*\MyNumberB)\relax}

\end{document} 

Output:

enter image description here

If you want to change the default number of decimal digits, use the option round-precision.

For example, adding to the \sisetup the option round-precision=3 you'll get 3 zeroes after the integer part.

share|improve this answer
    
the additional key round-precision = (default value 2) can then be used to change the number of digits after decimal mark. –  jfbu Apr 7 at 6:21
    
@jfbu Of course, thanks. Added to the answer. –  karlkoeller Apr 7 at 6:40

You can define a new command based on \num, exploiting the fact that siunitx uses expl3 internally:

\documentclass{article}
\usepackage[group-separator={,},group-minimum-digits=4]{siunitx}
% \usepackage{xparse} % already loaded by siunitx

\ExplSyntaxOn
\NewDocumentCommand{\numc}{ O{} m }
 {
  \num[#1]{ \fp_eval:n{ #2 } }
 }
\ExplSyntaxOff

\begin{document}

\newcommand{\MyNumberA}{40}
\newcommand{\MyNumberB}{60}

\numc{\MyNumberA*\MyNumberB}

\numc[
  group-separator={,},
  group-minimum-digits=4,
  round-integer-to-decimal,
  round-mode=places,
]{\MyNumberA*\MyNumberB}

\end{document}

As you see, local options can be specified for \numc in the same way as for \num.

enter image description here

share|improve this answer

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.