0

how to add numbers which will automatically ignore non-numbers, file should be read from .csv file

Eg.when A=10, B=20, C=NA --> here content of C contains some characters which is not a number so that number will not add to the sum
   sum=\A+\B+\C
   sum=30

   when A=10, B=20, C=5-->here content of C contains number so it will add that number to the sum
   sum=\A+\B+\C
   sum=35
TeXnician
  • 33,589
Biki Teron
  • 3,275

1 Answers1

1

Here is a first solution for integers, based on this post.

\documentclass{article} % from https://tex.stackexchange.com/questions/7180/testing-for-number
\usepackage{etoolbox}
\usepackage{calc}
\newcounter{return}
\makeatletter
\newcommand\ifnumber[1]{%
        \begingroup
        \edef\temp{#1}%
        \expandafter\ifstrempty\expandafter{\temp}
                {\endgroup\@secondoftwo}
                {\expandafter\ifnumber@i\temp\@nnil}%
}
\def\ifnumber@i#1#2\@nnil{%
        \if-#1%
                \ifstrempty{#2}
                        {\def\temp{X}}
                        {\def\temp{#2}}%
        \else
                \def\temp{#1#2}%
        \fi
        \afterassignment\ifnumhelper
        \count@0\temp\relax\@nnil
        \endgroup
}

\def\numrelax{\relax}%
\def\ifnumhelper#1\@nnil{%
        \def\temp{#1}%
        \ifx\temp\numrelax
                \aftergroup\@firstoftwo
        \else
                \aftergroup\@secondoftwo
        \fi
}
\makeatother

\newcommand\testnumber[1]{#1: \ifnumber{#1}{Number}{Not a number}\par}
\newcommand\addto[2]{\ifnumber{#1}{
\ifnumber{#2}{\setcounter{return}{#1+#2}}{
\setcounter{return}{#1}}}{\setcounter{return}{0}}
\thereturn}
\begin{document}
\def\foo{55}
\addto{10}{20}
\addto{10}{\foo}
\def\foo{abc}
\addto{10}{\foo}
\end{document}

UPDATE: This is a way to add an arbitrary number of numbers and strings, heavily relying on the answers here and here.

\documentclass{article} % from https://tex.stackexchange.com/questions/7180/testing-for-number
\usepackage{etoolbox}
\usepackage{calc}
\usepackage{xparse}
\newcounter{return}
\makeatletter
\newcommand\ifnumber[1]{%
        \begingroup
        \edef\temp{#1}%
        \expandafter\ifstrempty\expandafter{\temp}
                {\endgroup\@secondoftwo}
                {\expandafter\ifnumber@i\temp\@nnil}%
}
\def\ifnumber@i#1#2\@nnil{%
        \if-#1%
                \ifstrempty{#2}
                        {\def\temp{X}}
                        {\def\temp{#2}}%
        \else
                \def\temp{#1#2}%
        \fi
        \afterassignment\ifnumhelper
        \count@0\temp\relax\@nnil
        \endgroup
}

\def\numrelax{\relax}%
\def\ifnumhelper#1\@nnil{%
        \def\temp{#1}%
        \ifx\temp\numrelax
                \aftergroup\@firstoftwo
        \else
                \aftergroup\@secondoftwo
        \fi
}
\makeatother
\ExplSyntaxOn %from https://tex.stackexchange.com/questions/118114/commands-that-may-take-a-variable-number-of-arguments
\clist_new:N \l_myvararg_parameters_clist
\tl_new:N    \l_myvararg_current_item_tl
\NewDocumentCommand{\Add}{ m }
    {   \setcounter{return}{0}
        \clist_set:Nn \l_myvararg_parameters_clist { #1 }
        \int_while_do:nNnn { \clist_count:N \l_myvararg_parameters_clist } > { 1 }
            {
                \clist_pop:NN \l_myvararg_parameters_clist \l_myvararg_current_item_tl
                \tl_use:N \ifnumber{\l_myvararg_current_item_tl}{%
                \setcounter{return}{\thereturn+\l_myvararg_current_item_tl}}{}
            }   
         \clist_pop:NN \l_myvararg_parameters_clist
            \l_myvararg_current_item_tl
         {}   \tl_use:N \ifnumber{\l_myvararg_current_item_tl}{%
            \setcounter{return}{\thereturn+\l_myvararg_current_item_tl}}{}
    }
\ExplSyntaxOff


\newcommand\testnumber[1]{#1: \ifnumber{#1}{Number}{Not a number}\par}
\begin{document}
\def\foo{55}
\Add{1,2,3,\foo}\thereturn

\def\foo{abc}
\Add{1,2,3,\foo}\thereturn
\end{document}
  • How to add three numbers simultaneously@marmot – Biki Teron Dec 29 '17 at 06:17
  • @BikiTeron Please have a look at the update, it has an arguably better way of dealing with an arbitrary number of arguments. If you agree, I'll remove the first answer, i.e. the stuff before UPDATE. –  Dec 29 '17 at 19:26