You need to sign in to do that
Don't have an account?
Pallavi singh
Convert Currency to words
Hi,
i have an apex class which converts the currency field into words. But i want to add the Euro and Cent at the end like "One thousand Two hundred Thirty four Euro and Fifty six cent". Here is my Apex class and Trigger.
public with sharing class ConvertCurrencyToWords {
static String[] to_19 = new string[]{ 'zero', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven',
'Eight', 'Nine', 'Ten', 'Eleven', 'Twelve', 'Thirteen',
'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen' };
static String[] tens = new string[]{ 'Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety'};
static string[] denom = new string[]{ '',
'Thousand', 'Million', 'Billion', 'trillion', 'quadrillion',
'quintillion', 's!xtillion', 'septillion', 'octillion', 'nonillion',
'decillion', 'undecillion', 'duodecillion', 'tredecillion', 'quattuordecillion',
's!xdecillion', 'septendecillion', 'octodecillion', 'novemdecillion', 'vigintillion' };
// convert a value < 100 to English.
public static string convert_nn(integer val) {
if (val < 20)
return to_19[val];
if (val == 100)
return 'One Hundred';
for (integer v = 0; v < tens.size(); v++) {
String dcap = tens[v];
integer dval = 20 + 10 * v;
if (dval + 10 > val) {
if (Math.Mod(val,10) != 0)
return dcap + ' ' + to_19[Math.Mod(val,10)];
return dcap;
}
}
return 'Should never get here, less than 100 failure';
}
public static String convert_nnn(integer val) {
string word = '';
integer rem = val / 100;
integer mod = Math.mod(val,100);
if (rem > 0) {
word = to_19[rem] + ' Hundred and';
if (mod > 0) {
word += ' ';
}
}
if (mod > 0) {
word += convert_nn(mod);
}
return word;
}
public static String english_number(long val) {
if (val < 100) {
return convert_nn(val.intValue());
}
if (val < 1000) {
return convert_nnn(val.intValue());
}
for (integer v = 0; v < denom.size(); v++) {
integer didx = v - 1;
integer dval = (integer)Math.pow(1000, v);
if (dval > val) {
integer mod = (integer)Math.pow(1000, didx);
integer l = (integer) val / mod;
integer r = (integer) val - (l * mod);
String ret = convert_nnn(l) + ' ' + denom[didx];
if (r > 0) {
ret += ', ' + english_number(r);
}
return ret;
}
}
return 'Should never get here, bottomed out in english_number';
}
}
Trigger:
trigger ConvertCurrencyToWords on Opportunity (before insert, before update) {
for (Opportunity c : Trigger.new) {
if (c.PurchasePriceFormula__c != null && c.PurchasePriceFormula__c >= 0) {
Long n = c.PurchasePriceFormula__c.longValue();
string amo = ConvertCurrencyToWords.english_number(n);
string amo1 = amo.remove(',');
c.PurchasePriceWords__c = amo1;
} else {
c.PurchasePriceWords__c = null;
}
}
for (Opportunity c : Trigger.new) {
if (c.TotalPurchasePriceFormula__c != null && c.TotalPurchasePriceFormula__c >= 0) {
Long n = c.TotalPurchasePriceFormula__c.longValue();
string amo = ConvertCurrencyToWords.english_number(n);
string amo2 = amo.remove(',');
c.TotalPurchasePriceWords__c = amo2;
} else {
c.TotalPurchasePriceWords__c = null;
}
}
}
Thank you in advance
Pallavi
i have an apex class which converts the currency field into words. But i want to add the Euro and Cent at the end like "One thousand Two hundred Thirty four Euro and Fifty six cent". Here is my Apex class and Trigger.
public with sharing class ConvertCurrencyToWords {
static String[] to_19 = new string[]{ 'zero', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven',
'Eight', 'Nine', 'Ten', 'Eleven', 'Twelve', 'Thirteen',
'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen' };
static String[] tens = new string[]{ 'Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety'};
static string[] denom = new string[]{ '',
'Thousand', 'Million', 'Billion', 'trillion', 'quadrillion',
'quintillion', 's!xtillion', 'septillion', 'octillion', 'nonillion',
'decillion', 'undecillion', 'duodecillion', 'tredecillion', 'quattuordecillion',
's!xdecillion', 'septendecillion', 'octodecillion', 'novemdecillion', 'vigintillion' };
// convert a value < 100 to English.
public static string convert_nn(integer val) {
if (val < 20)
return to_19[val];
if (val == 100)
return 'One Hundred';
for (integer v = 0; v < tens.size(); v++) {
String dcap = tens[v];
integer dval = 20 + 10 * v;
if (dval + 10 > val) {
if (Math.Mod(val,10) != 0)
return dcap + ' ' + to_19[Math.Mod(val,10)];
return dcap;
}
}
return 'Should never get here, less than 100 failure';
}
public static String convert_nnn(integer val) {
string word = '';
integer rem = val / 100;
integer mod = Math.mod(val,100);
if (rem > 0) {
word = to_19[rem] + ' Hundred and';
if (mod > 0) {
word += ' ';
}
}
if (mod > 0) {
word += convert_nn(mod);
}
return word;
}
public static String english_number(long val) {
if (val < 100) {
return convert_nn(val.intValue());
}
if (val < 1000) {
return convert_nnn(val.intValue());
}
for (integer v = 0; v < denom.size(); v++) {
integer didx = v - 1;
integer dval = (integer)Math.pow(1000, v);
if (dval > val) {
integer mod = (integer)Math.pow(1000, didx);
integer l = (integer) val / mod;
integer r = (integer) val - (l * mod);
String ret = convert_nnn(l) + ' ' + denom[didx];
if (r > 0) {
ret += ', ' + english_number(r);
}
return ret;
}
}
return 'Should never get here, bottomed out in english_number';
}
}
Trigger:
trigger ConvertCurrencyToWords on Opportunity (before insert, before update) {
for (Opportunity c : Trigger.new) {
if (c.PurchasePriceFormula__c != null && c.PurchasePriceFormula__c >= 0) {
Long n = c.PurchasePriceFormula__c.longValue();
string amo = ConvertCurrencyToWords.english_number(n);
string amo1 = amo.remove(',');
c.PurchasePriceWords__c = amo1;
} else {
c.PurchasePriceWords__c = null;
}
}
for (Opportunity c : Trigger.new) {
if (c.TotalPurchasePriceFormula__c != null && c.TotalPurchasePriceFormula__c >= 0) {
Long n = c.TotalPurchasePriceFormula__c.longValue();
string amo = ConvertCurrencyToWords.english_number(n);
string amo2 = amo.remove(',');
c.TotalPurchasePriceWords__c = amo2;
} else {
c.TotalPurchasePriceWords__c = null;
}
}
}
Thank you in advance
Pallavi
To add the Euro and Cent at the end of the converted currency value, you can modify the english_number method of the ConvertCurrencyToWords class as follows:
Hope the above information helps !
Thank you.