Convert amount to words with Currency name in D365FO – X++
While designing an SSRS report recently, I had a reason to display the amount in words alongside the currency name. Example: 2299.68 = Two Thousand Two Hundred Ninety Nine Naira and Sixty Eight Kobo only.
Naira represents the currency of Nigeria and 100 Kobo = 1 Naira.
To achieve this, I create a method in my DP class as follow: I am passing 3 parameters: Amount, Naira and Kobo.
private Description convertAmountInWords(real _amount, str _currency1, str _currency2)
{
real decimals, WordReal;
int intNum;
str 250 word, decWord, wholeWord;
int repPos, repPos1, repPoswhole;
word = Global::numeralsToTxt_EN(_amount);
repPos = strscan(word, ' and', 1, strlen(word));
intNum = _amount;
decimals = _amount - intNum;
WordReal = _amount - decimals;
if (decimals == 0.00)
{
wholeWord = num2str(WordReal,0,0,0,0);
wholeWord = Global::numeralsToTxt_EN(str2num(wholeWord));
wholeWord = strdel(wholeWord, 1, 4);
repPoswhole = strscan(wholeWord,' and', 1, strlen(wholeWord));
wholeWord = substr(wholeWord, 1, repPoswhole-1);
word = strfmt("%1 %2 only",wholeWord,_currency1);
}
else
{
decWord = substr(num2str(decimals, 0, 2, 1, 1), 3, 2);
wholeWord = num2str(WordReal,0,0,0,0);
decWord = Global::numeralsToTxt_EN(str2num(decWord));
wholeWord = Global::numeralsToTxt_EN(str2num(wholeWord));
decWord = strdel(decWord, 1, 4);
wholeWord = strdel(wholeWord, 1, 4);
repPos1 = strscan(decWord, 'and', 1, strlen(decWord));
repPoswhole = strscan(wholeWord, ' and', 1, strlen(wholeWord));
decWord = substr(decWord, 1, repPos1-1);
wholeWord = substr(wholeWord, 1, repPoswhole-1);
word = strfmt("%1 %2 and %3 %4 only",wholeWord,_currency1,decWord,_currency2);
}
return word;
}