logo

Converting numbers into text in scripts

Sometimes for better user experience, you might need to convert numbers into text. The most typical case is writing a number: 152 – one hundred and fifty-two.

You can do it with the following script:

public void OnSummaChange(Context context, EleWise.ELMA.Model.Views.FormViewBuilder<Context> form)
{
    decimal mon;//there is no decimal data type in ELMA, but the RurPhrase works only with that data type, so you have to declare a decimal type variable
    mon = (decimal)context.Sum.Value;//to the created variable you add the value of the Sum variable with conersion to decimal
    context.InWords = P_AmountInWords_Scripts.RurPhrase(mon);//with the RurPhrase method you obtain the result and write it to the Write variable.
}
                             
public static string RurPhrase (decimal money)
{ 
    return CurPhrase(money,"euro");
}
 
public static string NumPhrase (ulong Value, bool IsMale) 
{
    if (Value==0UL) return "Zero";
    string[] Dek1={"","one","two"," three"," " four"," five"," six"," seven"," eight"," nine"," ten"," eleven"," twelve"," thirteen",
    " fourteen"," fifteen"," sixteen"," seventeen"," eighteen"," nineteen"};
    string[] Dek2={"",""," twenty"," thirty"," fourty"," fifty"," sixty"," seventy"," eighty"," ninety"};
    string[] Dek3={""," one hundred"," two hundred"," three hundred"," four hundred"," five hundred"," six hundred"," seven hundred"," eight hundred"," nine hundred"};
    string[] Th={"",""," thousand"," million"," billion"," trillion"," quadrillion"," quintillion"};
    string str="";
    for (byte th=1; Value>0; th++) 
    {
        ushort gr=(ushort)(Value%1000); 
        Value=(Value-gr)/1000;
        if (gr>0) 
        {
            byte d3=(byte)((gr-gr%100)/100);
            byte d1=(byte)(gr%10);
            byte d2=(byte)((gr-d3*100-d1)/10);
            if (d2==1) d1+=(byte)10;
            bool ismale=(th>2)||((th==1)&&IsMale);
            str=Dek3[d3]+Dek2[d2]+Dek1[d1]+EndDek1(d1,ismale)+Th[th]+EndTh(th,d1)+str;
        };
    };
    str=str.Substring(1,1).ToUpper()+str.Substring(2);
    return str;
}
 
#region Private members
 
private static string CurPhrase (decimal money, 
    string word1,string word234,string wordmore,
    string sword1,string sword234,string swordmore)
{ 
    money=decimal.Round(money,2);
    decimal decintpart=decimal.Truncate(money);
    ulong intpart=decimal.ToUInt64(decintpart);
    string str=NumPhrase(intpart,true)+" ";
    byte endpart=(byte)(intpart%100UL); 
    if (endpart>19) endpart=(byte)(endpart%10);
    switch (endpart) 
    {
        case 1: str+=word1; break;
        case 2:
        case 3:
        case 4: str+=word234; break;
        default: str+=wordmore; break;
    }
    byte fracpart=decimal.ToByte((money-decintpart)*100M);
    str+=" "+((fracpart<10)?"0":"")+fracpart.ToString()+" ";
    if (fracpart>19) fracpart=(byte)(fracpart%10);
    switch (fracpart) 
    {
        case 1: str+=sword1; break;
        case 2:
        case 3:
        case 4: str+=sword234; break;
        default: str+=swordmore; break;
    };
    return str;
}

#endregion

If this script is used, 1552 will be written as one thousand five hundred and fifty-two euro.

You can also create a method for converting between different currencies:
 
public static string USDPhrase (decimal money)
//where string is the type of data returned by the USDPhrase method, and decimal the document type of argument, money is the name of the argument
{return CurPhrase(money,"dollar";}
 
public static string GRVPhrase (decimal money)
{return CurPhrase(money,"rouble";}