如何将数字和字符相加?

如何将数字和字符相加?

如何添加数字,将自动忽略非数字,文件应从 .csv 文件中读取

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

答案1

这是整数的第一个解决方案,基于这个帖子

\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}

更新:这是一种添加任意数量的数字和字符串的方法,很大程度上依赖于答案这里这里

\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}

相关内容