EncompDEV
LoanDebugger2

Number Words Plugin

Click here to download this plugin with source code    

This plugin reads number from field [CX.NUMBERTOWORDS.IN] and outputs full spelling of the words for the number into field [CX.NUMBERTOWORDS.OUT]. This will allow you to convert numbers to words inside custom forms or field triggers using simple logic of setting Input field and reading Output field right after.

Main idea for conversion is copied from This StackOverflow Post.

Screenshot with Loan Monitor showing field changes:

Screenshot of Loan Debugger showing example field trigger:

Code used for this plugin:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using EllieMae.Encompass.ComponentModel;
using EllieMae.Encompass.Automation;
using EllieMae.Encompass.BusinessObjects.Loans;

namespace NumberWordsPlugin
{
    [Plugin]
    public class EncompassPlugin
    {
        const string FIELD_NUMBER_IN = "CX.NUMBERTOWORDS.IN";
        const string FIELD_WORDS_OUT = "CX.NUMBERTOWORDS.OUT";

        // Constructor - wait for Login event
        public EncompassPlugin()
        {
            EncompassApplication.Login += new EventHandler(Application_Login);
        }

        // Login - handle Loan Open and Loan Closing events
        private void Application_Login(object sender, EventArgs e)
        {
            EncompassApplication.LoanOpened += new EventHandler(LoanOpenedEvent);
            EncompassApplication.LoanClosing += new EventHandler(LoanClosingEvent);
        }

        // Loan Opened event - start monitoring fields
        private void LoanOpenedEvent(object sender, EventArgs e)
        {
            EncompassApplication.CurrentLoan.FieldChange += new FieldChangeEventHandler(Loan_FieldChange);
        }

        // Loan Closing event - stop monitoring fields
        private void LoanClosingEvent(object sender, EventArgs e)
        {
            EncompassApplication.CurrentLoan.FieldChange -= new FieldChangeEventHandler(Loan_FieldChange);
        }

        // Field Change event
        private void Loan_FieldChange(object source, FieldChangeEventArgs e)
        {
            if (e != null &&
                e.FieldID != null)
            {
                if (e.FieldID.Equals(FIELD_NUMBER_IN, StringComparison.OrdinalIgnoreCase))
                {
                    try
                    {
                        Loan loan = EncompassApplication.CurrentLoan;
                        int n = (int)loan.Fields[FIELD_NUMBER_IN].ToDecimal();
                        string sWords = NumberToWords(n).Trim();
                        Macro.SetFieldNoRules(FIELD_WORDS_OUT, sWords);
                    }
                    catch
                    {
                        // ignore errors but don't throw
                    }
                }
            }
        }

        // copied from http://stackoverflow.com/questions/2729752/converting-numbers-in-to-words-c-sharp
        public static string NumberToWords(int number)
        {
            if (number == 0)
            {
                return "Zero";
            }

            if (number < 0)
            {
                return "Minus " + NumberToWords(Math.Abs(number));
            }

            string words = "";

            if ((number / 1000000) > 0)
            {
                words += NumberToWords(number / 1000000) + " Million ";
                number %= 1000000;
            }

            if ((number / 1000) > 0)
            {
                words += NumberToWords(number / 1000) + " Thousand ";
                number %= 1000;
            }

            if ((number / 100) > 0)
            {
                words += NumberToWords(number / 100) + " Hundred ";
                number %= 100;
            }

            if (number > 0)
            {
                if (words != "")
                {
                    words += "and ";
                }

                string[] unitsMap = new[] { 
                    "Zero", "One", "Two", "Three", "Four", "Five", 
                    "Six", "Seven", "Eight", "Nine", "Ten", 
                    "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", 
                    "Sixteen", "Seventeen", "Eighteen", "Nineteen" };
                string[] tensMap = new[] { 
                    "Zero", "Ten", "Twenty", "Thirty", "Forty", "Fifty", 
                    "Sixty", "Seventy", "Eighty", "Ninety" };

                if (number < 20)
                    words += unitsMap[number];
                else
                {
                    words += tensMap[number / 10];
                    if ((number % 10) > 0)
                        words += "-" + unitsMap[number % 10];
                }
            }

            return words;
        }

    }
}