# How to add numbers with character?

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

• Please make your code snippet compilable! – Mensch Dec 29 '17 at 5:13
• This is completely unclear given that you've accepted the only answer here... – Werner Dec 29 '17 at 21:21

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}
\ifnumber{#2}{\setcounter{return}{#1+#2}}{
\setcounter{return}{#1}}}{\setcounter{return}{0}}
\thereturn}
\begin{document}
\def\foo{55}
\def\foo{abc}
\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
{   \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}