%%%
% Décomposition décimale
%%
\def\filedateDecDeci{2024/08/04}%
\def\fileversionDecDeci{0.1}%
\message{-- \filedateDecDeci\space v\fileversionDecDeci}%
%
\setKVdefault[DecompDeci]{Parentheses,Colore=false,CouleurU=Blue,CouleurD=Red,CouleurC=Green,Fleches=false,Details=false,ResultatSeul=false,SansMul=false}

\newcounter{PfCNbDecDeci}
\setcounter{PfCNbDecDeci}{0}

\makeatletter
\NewDocumentCommand\DecompositionDecimale{o m}{%
  \stepcounter{PfCNbDecDeci}%
  \useKVdefault[DecompDeci]%
  \setKV[DecompDeci]{#1}%
  % On cherche si le nombre est décimal ou entier
  \StrCount{#2}{.}[\PfC@NombrePoint]%
  \xintifboolexpr{\PfC@NombrePoint==0}{%
    % on compte la longueur.
    \StrLen{#2}[\PfC@NombrePuisMax]%
    \edef\PfC@RetiensPuis{0}%
    % On cherche le nombre de 0 terminaux.
    \xintFor* ##1 in{\xintSeq{1}{\PfC@NombrePuisMax}}\do{%
      \xintifboolexpr{\fpeval{#2/(10^##1)}==\fpeval{round(#2/(10^##1))}}{%
        \edef\PfC@RetiensPuis{\fpeval{\PfC@RetiensPuis+1}}%
      }{}%
    }%
    % On cherche un affichage du style \num de \siunitx
    \modulo{\fpeval{\PfC@NombrePuisMax}}{3}%
    \xintifboolexpr{\remainder==0}{%
      \edef\PfC@DecDeciEcart{0}
    }{\xintifboolexpr{\remainder==1}{\edef\PfC@DecDeciEcart{1}}{\edef\PfC@DecDeciEcart{2}}}%
    %%%%
    \ifboolKV[DecompDeci]{SansMul}{%
      \ensuremath{%
        \ifboolKV[DecompDeci]{ResultatSeul}{}{\num{#2}=}%
        \xintFor* ##1 in {\xintSeq{1}{\fpeval{\PfC@NombrePuisMax-\PfC@RetiensPuis}}}\do{%
          \StrChar{#2}{##1}[\PfC@NombreChiffre]
          \modulo{\fpeval{\PfC@NombrePuisMax+\PfC@DecDeciEcart--##1}}{3}%
          \xintifboolexpr{\PfC@NombreChiffre==0}{}{%
            \num{\fpeval{\PfC@NombreChiffre*10^(\PfC@NombrePuisMax-##1)}}%
            \xintifForLast{}{+}%
          }%
        }%
      }%
    }{%
      \ifboolKV[DecompDeci]{Fleches}{%
        \colorlet{oldcolor}{black}%
        % On affiche
        \begin{center}
          \xintFor* ##1 in {\xintSeq{1}{\fpeval{\PfC@NombrePuisMax}}}\do{%
            \StrChar{#2}{##1}[\PfC@NombreChiffre]%
            \modulo{\fpeval{\PfC@NombrePuisMax+\PfC@DecDeciEcart--##1}}{3}%
            \xintifboolexpr{\remainder==0}{%
              \color{\useKV[DecompDeci]{CouleurU}}\tikzmarknode[anchor=south,inner ysep=2pt]{DDA-\thePfCNbDecDeci-##1}{\PfC@NombreChiffre}{}\xintifForLast{}{\,\color{oldcolor}}}{%
              \xintifboolexpr{\remainder==1}{%
                \color{\useKV[DecompDeci]{CouleurC}}\tikzmarknode[anchor=south,inner ysep=2pt]{DDA-\thePfCNbDecDeci-##1}{\PfC@NombreChiffre}{}}{%
                \color{\useKV[DecompDeci]{CouleurD}}\tikzmarknode[anchor=south,inner ysep=2pt]{DDA-\thePfCNbDecDeci-##1}{\PfC@NombreChiffre}{}%
              }%
            }%
          }\color{oldcolor}%
          
          \vspace*{3em}
          
          \ifboolKV[DecompDeci]{Details}{%
            \ensuremath{%
              \xintFor* ##1 in {\xintSeq{1}{\fpeval{\PfC@NombrePuisMax-\PfC@RetiensPuis}}}\do{%
                \StrChar{#2}{##1}[\PfC@NombreChiffre]
                \modulo{\fpeval{\PfC@NombrePuisMax+\PfC@DecDeciEcart--##1}}{3}%
                \colorlet{oldcolor}{black}
                \xintifboolexpr{\remainder==0}{\color{\useKV[DecompDeci]{CouleurU}}}{%
                  \xintifboolexpr{\remainder==1}{\color{\useKV[DecompDeci]{CouleurC}}}{%
                    \color{\useKV[DecompDeci]{CouleurD}}%
                  }%
                }%
                \xintifboolexpr{\PfC@NombreChiffre==0}{}{%
                  \tikzmarknode[anchor=north,inner ysep=2pt]{DDC-\thePfCNbDecDeci-##1}{\boxed{\num{\fpeval{\PfC@NombreChiffre*10^(\PfC@NombrePuisMax-##1)}}}}\color{oldcolor}%
                  \xintifForLast{}{+}%
                }%
              }%
            }%
            
            \vspace*{3em}
            
          }{}%
          
          \ensuremath{%
            \xintFor* ##1 in {\xintSeq{1}{\fpeval{\PfC@NombrePuisMax-\PfC@RetiensPuis}}}\do{%
              \StrChar{#2}{##1}[\PfC@NombreChiffre]
              \xintifboolexpr{\PfC@NombreChiffre==0}{}{%
                \modulo{\fpeval{\PfC@NombrePuisMax+\PfC@DecDeciEcart--##1}}{3}%
                \colorlet{oldcolor}{.}
                \xintifboolexpr{\remainder==0}{\color{\useKV[DecompDeci]{CouleurU}}}{%
                  \xintifboolexpr{\remainder==1}{\color{\useKV[DecompDeci]{CouleurC}}}{%
                    \color{\useKV[DecompDeci]{CouleurD}}%
                  }%
                }%
                \tikzmarknode[anchor=north,inner ysep=2pt]{DDB-\thePfCNbDecDeci-##1}{\boxed{\PfC@NombreChiffre\times\num{\fpeval{10^(\PfC@NombrePuisMax-##1)}}}}{}\color{oldcolor}%
                \xintifForLast{}{+}%
              }%
            }%
          }%
        \end{center}
        \begin{tikzpicture}[remember picture,overlay]
          \xintFor* ##1 in {\xintSeq{1}{\fpeval{\PfC@NombrePuisMax-\PfC@RetiensPuis}}}\do{%
            \StrChar{#2}{##1}[\PfC@NombreChiffre]%
            \modulo{\fpeval{\PfC@NombrePuisMax+\PfC@DecDeciEcart--##1}}{3}%
            \xintifboolexpr{\remainder==0}{%
              \colorlet{PfCDecDeciColor}{\useKV[DecompDeci]{CouleurU}}}{%
              \xintifboolexpr{\remainder==1}{%
                \colorlet{PfCDecDeciColor}{\useKV[DecompDeci]{CouleurC}}}{%
                \colorlet{PfCDecDeciColor}{\useKV[DecompDeci]{CouleurD}}%
              }%
            }%
            \ifboolKV[DecompDeci]{Details}{%
              \xintifboolexpr{\PfC@NombreChiffre>0}{%
                \draw[-stealth,PfCDecDeciColor] (pic cs:DDA-\thePfCNbDecDeci-##1) to (pic cs:DDC-\thePfCNbDecDeci-##1);
                \node (DDD-\thePfCNbDecDeci-##1) at ($(pic cs:DDC-\thePfCNbDecDeci-##1)!0.35!(pic cs:DDB-\thePfCNbDecDeci-##1)$) {};
                \draw[-stealth,PfCDecDeciColor] (DDD-\thePfCNbDecDeci-##1) to (pic cs:DDB-\thePfCNbDecDeci-##1);
              }{}%
            }{%
              \xintifboolexpr{\PfC@NombreChiffre>0}{%
                \draw[-stealth,PfCDecDeciColor] (pic cs:DDA-\thePfCNbDecDeci-##1) to (pic cs:DDB-\thePfCNbDecDeci-##1);
              }{}%
            }%
          }%
        \end{tikzpicture}
      }{%
        \ifboolKV[DecompDeci]{Colore}{%
          \colorlet{oldcolor}{black}%
          \ensuremath{%
            \xintFor* ##1 in {\xintSeq{1}{\fpeval{\PfC@NombrePuisMax}}}\do{%
              \StrChar{#2}{##1}[\PfC@NombreChiffre]%
              \modulo{\fpeval{\PfC@NombrePuisMax+\PfC@DecDeciEcart--##1}}{3}%
              \xintifboolexpr{\remainder==0}{\color{\useKV[DecompDeci]{CouleurU}}\PfC@NombreChiffre\xintifForLast{}{\,\color{oldcolor}}}{%
                \xintifboolexpr{\remainder==1}{\color{\useKV[DecompDeci]{CouleurC}}\PfC@NombreChiffre}{%
                  \color{\useKV[DecompDeci]{CouleurD}}\PfC@NombreChiffre%
                }%
              }%
            }\color{oldcolor}%
            =        
            \xintFor* ##1 in {\xintSeq{1}{\fpeval{\PfC@NombrePuisMax-\PfC@RetiensPuis}}}\do{%
              \StrChar{#2}{##1}[\PfC@NombreChiffre]
              \xintifboolexpr{\PfC@NombreChiffre==0}{}{%
                \modulo{\fpeval{\PfC@NombrePuisMax-\PfC@RetiensPuis-##1}}{3}
                \colorlet{oldcolor}{.}
                \xintifboolexpr{\remainder==0}{\color{\useKV[DecompDeci]{CouleurU}}}{%
                  \xintifboolexpr{\remainder==1}{\color{\useKV[DecompDeci]{CouleurD}}}{%
                    \color{\useKV[DecompDeci]{CouleurC}}%
                  }%
                }%
                \boxed{\PfC@NombreChiffre\times\num{\fpeval{10**(\PfC@NombrePuisMax-##1)}}}\color{oldcolor}%
                \xintifForLast{}{+}%
              }%
            }%
          }
        }{%
          \ensuremath{%
            \ifboolKV[DecompDeci]{ResultatSeul}{}{\num{#2}=}%
            \xintFor* ##1 in {\xintSeq{1}{\fpeval{\PfC@NombrePuisMax-\PfC@RetiensPuis}}}\do{%
              \StrChar{#2}{##1}[\PfC@NombreChiffre]
              \xintifboolexpr{\PfC@NombreChiffre==0}{}{%
                \ifboolKV[DecompDeci]{Parentheses}{\left(}{}\PfC@NombreChiffre\times\num{\fpeval{10^(\PfC@NombrePuisMax-##1)}}\ifboolKV[DecompDeci]{Parentheses}{\right)}{}%
                \xintifForLast{}{+}%
              }%
            }%
          }%
        }%
      }%
    }%
  }{%partie avec les nombres décimaux
    \StrDel{#2}{.}[\PfC@RetiensNombreEntier]%
    \StrLen{#2}[\PfC@NombrePuisMax]%
    \edef\PfC@NombrePuisMax{\fpeval{\PfC@NombrePuisMax-1}}%
    \StrPosition{#2}{.}[\PfC@PositionPoint]%
    \edef\PfC@PuisDeci{\fpeval{\PfC@NombrePuisMax-\PfC@PositionPoint}}%
    \edef\PfC@RetiensPuis{0}%
    % On cherche le nombre de 0 terminaux.
    \xintFor* ##1 in{\xintSeq{1}{\PfC@NombrePuisMax}}\do{%
      \xintifboolexpr{\fpeval{(#2*10^(\PfC@PuisDeci+1))/(10^##1)}==\fpeval{round((#2*10^(\PfC@PuisDeci+1))/(10^##1))}}{%
        \edef\PfC@RetiensPuis{\fpeval{\PfC@RetiensPuis+1}}%
      }{}%
    }%
    %%
    \ifboolKV[DecompDeci]{SansMul}{%
      \ensuremath{%
        \ifboolKV[DecompDeci]{ResultatSeul}{}{\num{#2}=}%
        \xintFor* ##1 in {\xintSeq{1}{\PfC@NombrePuisMax-\PfC@RetiensPuis}}\do{%
          \StrChar{\PfC@RetiensNombreEntier}{##1}[\PfC@NombreChiffre]
          \xintifboolexpr{\PfC@NombreChiffre==0}{}{%
            \num{\fpeval{\PfC@NombreChiffre*10^(\PfC@NombrePuisMax-##1-\PfC@PuisDeci-1)}}%
            \xintifForLast{}{+}%
          }%
        }%
      }%
    }{%
      \ensuremath{%
        \ifboolKV[DecompDeci]{ResultatSeul}{}{\num{#2}=}%
        \xintFor* ##1 in {\xintSeq{1}{\PfC@NombrePuisMax-\PfC@RetiensPuis}}\do{%
          \StrChar{\PfC@RetiensNombreEntier}{##1}[\PfC@NombreChiffre]
          \xintifboolexpr{\PfC@NombreChiffre==0}{}{%
            \ifboolKV[DecompDeci]{Parentheses}{\left(}{}\PfC@NombreChiffre\times\num{\fpeval{10^(\PfC@NombrePuisMax-##1-\PfC@PuisDeci-1)}}\ifboolKV[DecompDeci]{Parentheses}{\right)}{}%
            \xintifForLast{}{+}%
          }%
        }%
      }%
    }%
  }%
}%