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

\NewDocumentCommand\DecompositionDecimale{o m}{%
  \stepcounter{PfCNbDecDeci}%
  \useKVdefault[DecompDeci]%
  \setKV[DecompDeci]{#1}%
  % On cherche si le nombre est décimal ou entier
  \StrCount{#2}{.}[\PfCNombrePoint]%
  \xintifboolexpr{\PfCNombrePoint==0}{%
    % on compte la longueur.
    \StrLen{#2}[\PfCNombrePuisMax]%
    \xdef\PfCRetiensPuis{0}%
    % On cherche le nombre de 0 terminaux.
    \xintFor* ##1 in{\xintSeq{1}{\PfCNombrePuisMax}}\do{%
      \xintifboolexpr{\fpeval{#2/(10^##1)}==\fpeval{round(#2/(10^##1))}}{%
        \xdef\PfCRetiensPuis{\fpeval{\PfCRetiensPuis+1}}%
      }{}%
    }%
    % On cherche un affichage du style \num de \siunitx
    \modulo{\fpeval{\PfCNombrePuisMax}}{3}%
    \xintifboolexpr{\remainder==0}{%
      \xdef\PfCDecDeciEcart{0}
    }{\xintifboolexpr{\remainder==1}{\xdef\PfCDecDeciEcart{1}}{\xdef\PfCDecDeciEcart{2}}}%
    %%%%
    \ifboolKV[DecompDeci]{SansMul}{%
      \ensuremath{%
        \ifboolKV[DecompDeci]{ResultatSeul}{}{\num{#2}=}%
        \xintFor* ##1 in {\xintSeq{1}{\fpeval{\PfCNombrePuisMax-\PfCRetiensPuis}}}\do{%
          \StrChar{#2}{##1}[\PfCNombreChiffre]
          \modulo{\fpeval{\PfCNombrePuisMax+\PfCDecDeciEcart--##1}}{3}%
          % \colorlet{oldcolor}{.}
          %\xintifboolexpr{\remainder==0}{\color{\useKV[DecompDeci]{CouleurU}}}{%
          %  \xintifboolexpr{\remainder==1}{\color{\useKV[DecompDeci]{CouleurC}}}{%
          %    \color{\useKV[DecompDeci]{CouleurD}}%
          %  }%
          %}%
          \xintifboolexpr{\PfCNombreChiffre==0}{}{%
            \num{\fpeval{\PfCNombreChiffre*10^(\PfCNombrePuisMax-##1)}}%\color{oldcolor}%
            \xintifForLast{}{+}%
          }%
        }%
      }%
    }{%
      \ifboolKV[DecompDeci]{Fleches}{%
        \colorlet{oldcolor}{black}%
        % On affiche
        \begin{center}
          \xintFor* ##1 in {\xintSeq{1}{\fpeval{\PfCNombrePuisMax}}}\do{%
            \StrChar{#2}{##1}[\PfCNombreChiffre]%
            \modulo{\fpeval{\PfCNombrePuisMax+\PfCDecDeciEcart--##1}}{3}%
            \xintifboolexpr{\remainder==0}{%
              \color{\useKV[DecompDeci]{CouleurU}}\tikzmarknode[anchor=south,inner ysep=2pt]{DDA-\thePfCNbDecDeci-##1}{\PfCNombreChiffre}{}\xintifForLast{}{\,\color{oldcolor}}}{%
              \xintifboolexpr{\remainder==1}{%
                \color{\useKV[DecompDeci]{CouleurC}}\tikzmarknode[anchor=south,inner ysep=2pt]{DDA-\thePfCNbDecDeci-##1}{\PfCNombreChiffre}{}}{%
                \color{\useKV[DecompDeci]{CouleurD}}\tikzmarknode[anchor=south,inner ysep=2pt]{DDA-\thePfCNbDecDeci-##1}{\PfCNombreChiffre}{}%
              }%
            }%
          }\color{oldcolor}%
          
          \vspace*{3em}
          
          \ifboolKV[DecompDeci]{Details}{%
            \ensuremath{%
              \xintFor* ##1 in {\xintSeq{1}{\fpeval{\PfCNombrePuisMax-\PfCRetiensPuis}}}\do{%
                \StrChar{#2}{##1}[\PfCNombreChiffre]
                \modulo{\fpeval{\PfCNombrePuisMax+\PfCDecDeciEcart--##1}}{3}%
                \colorlet{oldcolor}{black}
                \xintifboolexpr{\remainder==0}{\color{\useKV[DecompDeci]{CouleurU}}}{%
                  \xintifboolexpr{\remainder==1}{\color{\useKV[DecompDeci]{CouleurC}}}{%
                    \color{\useKV[DecompDeci]{CouleurD}}%
                  }%
                }%
                \xintifboolexpr{\PfCNombreChiffre==0}{}{%
                  \tikzmarknode[anchor=north,inner ysep=2pt]{DDC-\thePfCNbDecDeci-##1}{\boxed{\num{\fpeval{\PfCNombreChiffre*10^(\PfCNombrePuisMax-##1)}}}}\color{oldcolor}%
                  \xintifForLast{}{+}%
                }%
              }%
            }%
            
            \vspace*{3em}
            
          }{}%
          
          \ensuremath{%
            \xintFor* ##1 in {\xintSeq{1}{\fpeval{\PfCNombrePuisMax-\PfCRetiensPuis}}}\do{%
              \StrChar{#2}{##1}[\PfCNombreChiffre]
              \xintifboolexpr{\PfCNombreChiffre==0}{}{%
                \modulo{\fpeval{\PfCNombrePuisMax+\PfCDecDeciEcart--##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{\PfCNombreChiffre\times\num{\fpeval{10^(\PfCNombrePuisMax-##1)}}}}{}\color{oldcolor}%
                \xintifForLast{}{+}%
              }%
            }%
          }%
        \end{center}
        \begin{tikzpicture}[remember picture,overlay]
          \xintFor* ##1 in {\xintSeq{1}{\fpeval{\PfCNombrePuisMax-\PfCRetiensPuis}}}\do{%
            \StrChar{#2}{##1}[\PfCNombreChiffre]%
            \modulo{\fpeval{\PfCNombrePuisMax+\PfCDecDeciEcart--##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{\PfCNombreChiffre>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{\PfCNombreChiffre>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{\PfCNombrePuisMax}}}\do{%
              \StrChar{#2}{##1}[\PfCNombreChiffre]%
              \modulo{\fpeval{\PfCNombrePuisMax+\PfCDecDeciEcart--##1}}{3}%
              \xintifboolexpr{\remainder==0}{\color{\useKV[DecompDeci]{CouleurU}}\PfCNombreChiffre\xintifForLast{}{\,\color{oldcolor}}}{%
                \xintifboolexpr{\remainder==1}{\color{\useKV[DecompDeci]{CouleurC}}\PfCNombreChiffre}{%
                  \color{\useKV[DecompDeci]{CouleurD}}\PfCNombreChiffre%
                }%
              }%
            }\color{oldcolor}%
            =        
            \xintFor* ##1 in {\xintSeq{1}{\fpeval{\PfCNombrePuisMax-\PfCRetiensPuis}}}\do{%
              \StrChar{#2}{##1}[\PfCNombreChiffre]
              \xintifboolexpr{\PfCNombreChiffre==0}{}{%
                \modulo{\fpeval{\PfCNombrePuisMax-\PfCRetiensPuis-##1}}{3}
                \colorlet{oldcolor}{.}
                \xintifboolexpr{\remainder==0}{\color{\useKV[DecompDeci]{CouleurU}}}{%
                  \xintifboolexpr{\remainder==1}{\color{\useKV[DecompDeci]{CouleurD}}}{%
                    \color{\useKV[DecompDeci]{CouleurC}}%
                  }%
                }%
                \boxed{\PfCNombreChiffre\times\num{\fpeval{10^(\PfCNombrePuisMax-##1)}}}\color{oldcolor}%
                \xintifForLast{}{+}%
              }%
            }%
          }
        }{%
          \ensuremath{%
            \ifboolKV[DecompDeci]{ResultatSeul}{}{\num{#2}=}%
            \xintFor* ##1 in {\xintSeq{1}{\fpeval{\PfCNombrePuisMax-\PfCRetiensPuis}}}\do{%
              \StrChar{#2}{##1}[\PfCNombreChiffre]
              \xintifboolexpr{\PfCNombreChiffre==0}{}{%
                \ifboolKV[DecompDeci]{Parentheses}{\left(}{}\PfCNombreChiffre\times\num{\fpeval{10^(\PfCNombrePuisMax-##1)}}\ifboolKV[DecompDeci]{Parentheses}{\right)}{}%
                \xintifForLast{}{+}%
              }%
            }%
          }%
        }%
      }%
    }%
  }{%partie avec les nombres décimaux
    \StrDel{#2}{.}[\PfCRetiensNombreEntier]%
    \StrLen{#2}[\PfCNombrePuisMax]%
    \xdef\PfCNombrePuisMax{\fpeval{\PfCNombrePuisMax-1}}%
    \StrPosition{#2}{.}[\PfCPositionPoint]%
    \xdef\PfCPuisDeci{\fpeval{\PfCNombrePuisMax-\PfCPositionPoint}}%
    \xdef\PfCRetiensPuis{0}%
    % On cherche le nombre de 0 terminaux.
    \xintFor* ##1 in{\xintSeq{1}{\PfCNombrePuisMax}}\do{%
      \xintifboolexpr{\fpeval{(#2*10^(\PfCPuisDeci+1))/(10^##1)}==\fpeval{round((#2*10^(\PfCPuisDeci+1))/(10^##1))}}{%
        \xdef\PfCRetiensPuis{\fpeval{\PfCRetiensPuis+1}}%
      }{}%
    }%
    %%
    \ifboolKV[DecompDeci]{SansMul}{%
      \ensuremath{%
        \ifboolKV[DecompDeci]{ResultatSeul}{}{\num{#2}=}%
        \xintFor* ##1 in {\xintSeq{1}{\PfCNombrePuisMax-\PfCRetiensPuis}}\do{%
          \StrChar{\PfCRetiensNombreEntier}{##1}[\PfCNombreChiffre]
          \xintifboolexpr{\PfCNombreChiffre==0}{}{%
            \num{\fpeval{\PfCNombreChiffre*10^(\PfCNombrePuisMax-##1-\PfCPuisDeci-1)}}%
            \xintifForLast{}{+}%
          }%
        }%
      }%
    }{%
      \ensuremath{%
        \ifboolKV[DecompDeci]{ResultatSeul}{}{\num{#2}=}%
        \xintFor* ##1 in {\xintSeq{1}{\PfCNombrePuisMax-\PfCRetiensPuis}}\do{%
          \StrChar{\PfCRetiensNombreEntier}{##1}[\PfCNombreChiffre]
          \xintifboolexpr{\PfCNombreChiffre==0}{}{%
            \ifboolKV[DecompDeci]{Parentheses}{\left(}{}\PfCNombreChiffre\times\num{\fpeval{10^(\PfCNombrePuisMax-##1-\PfCPuisDeci-1)}}\ifboolKV[DecompDeci]{Parentheses}{\right)}{}%
            \xintifForLast{}{+}%
          }%
        }%
      }%
    }%
  }%
}%