Skip to content

Instantly share code, notes, and snippets.

@Mohammedkb
Created March 18, 2022 20:53
Show Gist options
  • Save Mohammedkb/94f2467c2db8a4197dd546db979848c4 to your computer and use it in GitHub Desktop.
Save Mohammedkb/94f2467c2db8a4197dd546db979848c4 to your computer and use it in GitHub Desktop.
Lambda code to be imported in Excel Advanced Formula Environment to build a customized function that converts numbers to words.
NumberToWords = LAMBDA(DNumValue, CUR, DecimalCUR, DecimalCURPlace,
LET(
NumValue, INT(DNumValue),
Words1,IFS(
len(NumValue)=1,GetDigit(value(NumValue)) & " " & CUR,
len(NumValue)=2,GetTens(value(NumValue))& " " & CUR,
len(NumValue)=3,GetHundreds(value(NumValue))& " " & CUR,
len(NumValue)<=6,GetHundreds(VALUE(left(NumValue,len(NumValue)-3))) & " Thousand " & GetHundreds(value(NumValue))& " " & CUR,
len(NumValue)<=9,GetHundreds(VALUE(left(NumValue,len(NumValue)-6))) & " Million " & GetHundreds(VALUE(left(NumValue,len(NumValue)-3))) & " Thousand " & GetHundreds(value(NumValue))& " " & CUR,
len(NumValue)<=12,GetHundreds(VALUE(left(NumValue,len(NumValue)-9))) & " Billion " & GetHundreds(VALUE(left(NumValue,len(NumValue)-6))) & " Million " & GetHundreds(VALUE(left(NumValue,len(NumValue)-3))) & " Thousand " & GetHundreds(value(NumValue))& " " & CUR),
Words, Words1 & " "& GetDecimal(DNumValue,DecimalCURPlace,DecimalCUR),
Words
)
);
GetDecimal =LAMBDA(NumberVal,DecimalCURPlace, DecimalCUR,
LET(
DecPlace, IFERROR(SEARCH(".",VALUE(NumberVal),1),0),
DecP, IF(DecPlace>0,"and "& GetHundreds(Left(Mid(VALUE(NumberVal), DecPlace + 1,DecimalCURPlace) & "00", DecimalCURPlace)) & " " & DecimalCUR,""),
DecP
)
);
GetHundreds = LAMBDA(HundNum,
LET(
HundNum, RIGHT("000" & HundNum,3),
Hundreds0, If(value(Mid(HundNum, 1, 1)) <> 0, GetDigit(value(Mid(HundNum, 1, 1))) & " Hundred ",),
Hundreds, If(value(Mid(HundNum, 2, 1)) <> 0, Hundreds0 & GetTens(value(Mid(HundNum, 2,2))),Hundreds0 & GetDigit(value(Mid(HundNum, 3,3)))),
Hundreds
)
);
GetDigit = LAMBDA(Digit,
LET(
TDigit, SWITCH(Digit,1,"One",2,"Two",3,"Three",4,"Four",5,"Five",6,"Six",7,"Seven",8,"Eight",9,"Nine",""),
TDigit
));
GetTens = LAMBDA(Tens,
LET(
TTens, IF(value(LEFT(Tens,1))=1,
SWITCH(Tens,10,"Ten",11,"Eleven",12,"Twelve",13,"Thirteen",14,"Fourteen",15,"Fifteen",16,"Sixteen",17,"Seventeen",18,"Eighteen",19,"Nineteen"),
TEXTJOIN(" ",,SWITCH(value(LEFT(Tens,1)),2,"Twenty",3,"Thirty",4,"Forty",5,"Fifty",6,"Sixty",7,"Seventy",8,"Eighty",9,"Ninety"),GetDigit(value(RIGHT(Tens,1))))
),
TTens
)
);
@ExcelRobot
Copy link

Very cool lambda! Nice work!

@jaguarxii
Copy link

Hi, I would like to upload the translation to spanish of the "words"
`NumberToWords =LAMBDA(DNumValue; CUR; DecimalCUR; DecimalCURPlace;
LET(
NumValue; INT(DNumValue);
Words1;IFS(
len(NumValue)=1;_getDigit(value(NumValue)) & " " & CUR;
len(NumValue)=2;_getTens(value(NumValue))& " " & CUR;
len(NumValue)=3;_getHundreds(value(NumValue))& " " & CUR;
len(NumValue)<=6;_getHundreds(VALUE(left(NumValue;len(NumValue)-3))) & " Mil " & _getHundreds(value(NumValue))& " " & CUR;
len(NumValue)<=9;_getHundreds(VALUE(left(NumValue;len(NumValue)-6))) & " Millones " & _getHundreds(VALUE(left(NumValue;len(NumValue)-3))) & " Mil " & _getHundreds(value(NumValue))& " " & CUR;
len(NumValue)<=12;_getHundreds(VALUE(left(NumValue;len(NumValue)-9))) & " Billones " & _getHundreds(VALUE(left(NumValue;len(NumValue)-6))) & " Millones " & _getHundreds(VALUE(left(NumValue;len(NumValue)-3))) & " Thousand " & _getHundreds(value(NumValue))& " " & CUR);
Words; Words1 & " "& _getDecimal(DNumValue;DecimalCURPlace;DecimalCUR);
Words
)
)

_getDecimal =LAMBDA(NumberVal; DecimalCURPlace; DecimalCUR; LET( DecPlace; IFERROR(SEARCH(".";VALUE(NumberVal);1);0); DecP; IF(DecPlace>0;"y "& _getTens(Left(Mid(VALUE(NumberVal); DecPlace + 1;DecimalCURPlace) & "00"; DecimalCURPlace)) & " " & DecimalCUR;""); DecP ) )

_getHundreds =LAMBDA(HundNum; LET( HundNum; RIGHT("000" & HundNum;3); Hundreds0; switch(value(Mid(HundNum; 1; 1));1;" Ciento ";If(value(Mid(HundNum; 1; 1)) <> 0; _getDigit(value(Mid(HundNum; 1; 1))) & " Cientos ";)); Hundreds; If(value(Mid(HundNum; 2; 1)) <> 0; Hundreds0 & _getTens(value(Mid(HundNum; 2;2)));Hundreds0 & _getDigit(value(Mid(HundNum; 3;3)))); Hundreds ) )

_getDigit =LAMBDA(Digit;LET(TDigit; SWITCH(Digit;1;"Uno";2;"Dos";3;"Tres";4;"Cuatro";5;"Cinco";6;"Seis";7;"Siete";8;"Ocho";9;"Nueve";"");TDigit))

_getTens =LAMBDA(Tens; LET( TTens; IF(value(LEFT(Tens;1))=1; SWITCH(Tens;10;"Diez";11;"Once";12;"Doce";13;"Trece";14;"Catorce";15;"Quince";16;"Dieciséis";17;"Diecisiete";18;"Dieciocho";19;"Diecinueve"); TEXTJOIN(" ";;SWITCH(value(LEFT(Tens;1));2;"Veinte";3;"Treinta";4;"Cuarenta";5;"Cincuenta";6;"Sesenta";7;"Setenta";8;"Ochenta";9;"Noventa");concat(if(value(RIGHT(Tens;1))<>0;"y ";"");_getDigit(value(RIGHT(Tens;1))))) ); TTens ) )

`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment