6

I want to compare the number with different format, but do not get the desired result.

\documentclass[a4paper]{article}
\usepackage{ifthen}
\usepackage{numprint}
\npthousandsep{,}
\npdecimalsign{.}
\nprounddigits{2}

\newcommand{\dotnumber}{1200.00}
\newcommand{\nodotnumber}{1200}

\begin{document}
\ifthenelse{\equal{\numprint{\dotnumber}}{\numprint{\nodotnumber}}}{true}{false}\\
\numprint{\dotnumber}\\
\numprint{\nodotnumber}\\
\end{document}

Here is the result: result

What is the problem there? Are there any methods to get the result to be true?

6

You are not comparing two numbers, but the set of instructions to print them; so the test cannot succeed.

This is a job for expl3 and its floating point module.

\documentclass[a4paper]{article}

\usepackage{xparse}
\usepackage{numprint}

\ExplSyntaxOn
\DeclareExpandableDocumentCommand{\numcompare}{O{=}mmmm}
 {
  \fp_compare:nTF { #2 #1 #3 } { #4 } { #5 }
 }
\ExplSyntaxOff

\npthousandsep{,}
\npdecimalsign{.}
\nprounddigits{2}

\newcommand{\dotnumber}{1200.00}
\newcommand{\nodotnumber}{1200}

\begin{document}

\numcompare{\dotnumber}{\nodotnumber}{true}{false}

\numprint{\dotnumber}

\numprint{\nodotnumber}

Expected: true; true, true, false

\numcompare{1.2e2}{120}{true}{false};
\numcompare[>]{10}{9}{true}{false};
\numcompare[>=]{10}{10}{true}{false};
\numcompare[<]{10}{9}{true}{false}

\end{document}

The function \fp_compare:nTF performs the test on the supplied numbers (and the supplied relation sign, if the default = is not wanted); the numbers should be in a format according to IEEE standards for floating points.

enter image description here

3

You can do testing the part before dot and the part after dot separately.

Edit If you need to do other tests (< or >, not only =), then the "after-dot" part must be re-calculated using \tnC macro. Zeros are appended to nine digits in total by this macro. If there are more than nine digits in the "after-dot" part then the rest is simply ignored. So, exact calculation is possible to nine digits after decimal dot only. This is due to the maximal integer in TeX is cca 2*10^9. If you need more exact calculation then use \input apnum or some similar.

\newif\iftestnumber
\def\testnumbers#1#2#3#4{%
   \edef\tmpA{\expandafter\tnA#1.;}\edef\tmpB{\expandafter\tnA#3.;}%
   \edef\tmpAA{\expandafter\tnAA#1.;}\edef\tmpBB{\expandafter\tnAA#3.;}%
   \def\tmp{}\expandafter\tnC\tmpAA!;000000000!;\let\tmpAA=\tmp
   \def\tmp{}\expandafter\tnC\tmpBB!;000000000!;\let\tmpBB=\tmp
   \testnumberfalse
   \ifx=#2\ifnum\tmpA=\tmpB\space \ifnum\tmpAA=\tmpBB\space \testnumbertrue\fi\fi
   \else \ifnum\tmpA#2\tmpB\space \testnumbertrue
         \else \ifnum\tmpA=\tmpB\space \ifnum\tmpAA#2\tmpBB\space \testnumbertrue
   \fi   \fi
   \iftestnumber
}
\def\tnA#1.#2;{\ifx;#1;0\else#1\fi}
\def\tnAA#1.#2;{\ifx;#2;0\else \tnA#2;\fi}
\def\tnC#1#2;#3#4;{\ifx!#3\def\next##1;##2;{}\else
   \ifx!#1\edef\tmp{\tmp#3}\def\next{\tnC!}%
   \else\edef\tmp{\tmp#1}\let\next=\tnC\fi\fi
   \next#2;#4;%
}

\def\dotnumber{1200.00}
\def\nodotnumber{1200}

\testnumbers \dotnumber=\nodotnumber \iftrue True\else False\fi

\testnumbers {2.009} < {2.012} \iftrue True\else False\fi
1

You can use dimension :

\documentclass[a4paper]{article}
\usepackage{ifthen}
\usepackage{numprint}
\npthousandsep{,}
\npdecimalsign{.}
\nprounddigits{2}

\newcommand{\dotnumber}{1200.00}
\newcommand{\nodotnumber}{1200}

\begin{document}
\ifthenelse{\equal{\numprint{\dotnumber}}{\numprint{\nodotnumber}}}{true}{false}\\
\ifthenelse{\lengthtest{\dotnumber pt=\nodotnumber pt}}{true}{false}\\
\numprint{\dotnumber}\\
\numprint{\nodotnumber}\\
\end{document}

enter image description here

3
  • The problem of this method is that large number is not allowed, or the LaTeX will show errors. Any solution?
    – Joe
    Apr 4 '16 at 5:40
  • @Joe Could you show an example ?
    – flav
    Apr 4 '16 at 6:59
  • For example, 120000
    – Joe
    Apr 4 '16 at 8:48

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.