Class 在apex salesforce中将数字转换为单词
下面的apex代码可用于将数字(货币)转换为文字。此代码可以在触发器、visualforce页面中使用,以文字形式转换任何数字/货币字段值,并存储在任何文本字段中 这门课怎么叫Class 在apex salesforce中将数字转换为单词,class,salesforce,visualforce,apex,Class,Salesforce,Visualforce,Apex,下面的apex代码可用于将数字(货币)转换为文字。此代码可以在触发器、visualforce页面中使用,以文字形式转换任何数字/货币字段值,并存储在任何文本字段中 这门课怎么叫 Decimal d = 1491511.61; NumberTOWordConvertion nwcObj = new NumberTOWordConvertion(); String numInWords = nwcObj.getNumberTOWordConvertion(d); 产量:壹仟肆佰玖万壹仟伍佰壹拾壹卢
Decimal d = 1491511.61;
NumberTOWordConvertion nwcObj = new NumberTOWordConvertion();
String numInWords = nwcObj.getNumberTOWordConvertion(d);
产量:壹仟肆佰玖万壹仟伍佰壹拾壹卢比陆拾壹派萨整
public class NumberTOWordConvertion {
// Call this method with Number to convert
public String getNumberTOWordConvertion(Decimal num) {
Decimal junkVal = num;
Decimal junkValPaisa = junkVal - Math.floor(junkVal);
junkVal = Math.floor(junkVal);
String obStr = junkVal.toPlainString();
String[] numReversed = obStr.split('');
String[] actnumber = reverse(numReversed);
String firstHalf = convertInWords(numReversed, actnumber);
Integer tmp = Math.round(junkValPaisa * 100);
junkValPaisa = (Decimal)tmp / 100; System.debug('jj :' + junkValPaisa);
String paisaStr = junkValPaisa.toPlainString();
String secondHalf;
if (paisaStr == '0') {
secondHalf = '';
} else if (paisaStr.length() != 4) {
paisaStr = paisaStr + '0';
paisaStr = paisaStr.substring(2);
String [] numReversedPaisa = paisaStr.split('');
String[] actnumberPaisa = reverse(numReversedPaisa);
secondHalf = convertInWords(numReversedPaisa, actnumberPaisa);
} else {
paisaStr = paisaStr.substring(2);
String [] numReversedPaisa = paisaStr.split('');
String[] actnumberPaisa = reverse(numReversedPaisa);
secondHalf = convertInWords(numReversedPaisa, actnumberPaisa);
}
String SumOFHalves = '';
if (secondHalf.length() > 4) {
firstHalf = firstHalf.replace('Only', 'Rupess And ');
secondHalf = secondHalf.replace('Only', 'Paisa Only');
SumOFHalves = firstHalf + secondHalf;
} else {
firstHalf = firstHalf.replace('Only', 'Rupess Only');
SumOFHalves = firstHalf;
}
// IF amount has any value
if (SumOFHalves.length() > 5) {
return SumOFHalves;
} else {
return '';
}
}
// Method reverse the number
public List<String> reverse(List<String> strToRev) {
List<String> revList = new List<String>();
for (Integer i = strToRev.size() - 1; i >= 0; i--) {
revList.add(strToRev.get(i));
}
revList.add('');
return revList;
}
public String convertInWords(String[] numRev, String[] actnum) {
List<String> iWords = new List<String> {'Zero', ' One', ' Two', ' Three', ' Four', ' Five', ' Six', ' Seven', ' Eight', ' Nine'};
List<String> ePlace = new List<String> {' Ten', ' Eleven', ' Twelve', ' Thirteen', ' Fourteen', ' Fifteen', ' Sixteen', ' Seventeen', ' Eighteen', ' Nineteen'};
List<String> tensPlace = new List<String> {'dummy', ' Ten', ' Twenty', ' Thirty', ' Forty', ' Fifty', ' Sixty', ' Seventy', ' Eighty', ' Ninety' };
Integer iWordsLength = numRev.size();
String totalWords = '';
List<String> inWords = new List<String>();
for (Integer k = 0; k < iWordsLength; k++) {
inWords.add('');
}
String finalWord = '';
Integer j = 0;
// Main For loop
for (Integer i = 0; i < iWordsLength; i++) {
if (i == 0) {
if (actnum[i] == '0' || actnum[i + 1] == '1') {
inWords[j] = '';
} else {
inWords[j] = iWords[Integer.valueof(actnum[i])];
}
inWords[j] = inWords[j] + ' Only';
} else if (i == 1) {
if (actnum[i] == '0') {
inWords[j] = '';
} else if (actnum[i] == '1') {
inWords[j] = ePlace[Integer.valueof(actnum[i - 1])];
} else {
inWords[j] = tensPlace[Integer.valueof(actnum[i])];
}
} else if (i == 2) {
if (actnum[i] == '0') {
inWords[j] = '';
} else if (actnum[i - 1] != '0' && actnum[i - 2] != '0') {
inWords[j] = iWords[Integer.valueof(actnum[i])] + ' Hundred and';
} else {
inWords[j] = iWords[Integer.valueof(actnum[i])] + ' Hundred';
}
} else if (i == 3) {
if (actnum[i] == '0' || actnum[i + 1] == '1') {
inWords[j] = '';
} else {
inWords[j] = iWords[Integer.valueof(actnum[i])];
}
if (actnum[i + 1] != '0' || Integer.valueof(actnum[i]) > 0) {
inWords[j] = inWords[j] + ' Thousand';
}
} else if (i == 4) {
if (actnum[i] == '0') {
inWords[j] = '';
} else if (actnum[i] == '1') {
inWords[j] = ePlace[Integer.valueof(actnum[i - 1])];
} else {
inWords[j] = tensPlace[Integer.valueof(actnum[i])];
}
} else if (i == 5) {
if (actnum[i] == '0' || actnum[i + 1] == '1') {
inWords[j] = '';
} else {
inWords[j] = iWords[Integer.valueof(actnum[i])];
}
if (actnum[i + 1] != '0' || Integer.valueof(actnum[i]) > 0) {
inWords[j] = inWords[j] + ' Lakh';
}
} else if (i == 6) {
if (actnum[i] == '0') {
inWords[j] = '';
} else if (actnum[i] == '1') {
inWords[j] = ePlace[Integer.valueof(actnum[i - 1])];
} else {
inWords[j] = tensPlace[Integer.valueof(actnum[i])];
}
} else if (i == 7) {
if (actnum[i] == '0' || actnum[i + 1] == '1' ) {
inWords[j] = '';
} else {
inWords[j] = iWords[Integer.valueof(actnum[i])];
}
inWords[j] = inWords[j] + ' Crore';
} else if (i == 8) {
if (actnum[i] == '0') {
inWords[j] = '';
} else if (actnum[i] == '1') {
inWords[j] = ePlace[Integer.valueof(actnum[i - 1])];
} else {
inWords[j] = tensPlace[Integer.valueof(actnum[i])];
}
}
j++;
}
// End of For loop
// Reverse the List
inWords = reverse(inWords);
for (Integer i = 0; i < inWords.size(); i++) {
finalWord += inWords[i];
}
return finalWord;
}
}
公共类numbertoword转换{
//使用要转换的数字调用此方法
公共字符串GetNumberToWordConversion(十进制数){
十进制junkVal=num;
十进制junkValPaisa=junkVal-数学地板(junkVal);
junkVal=数学楼层(junkVal);
字符串obStr=junkVal.toPlainString();
字符串[]numReversed=obStr.split(“”);
字符串[]actnumber=反向(numReversed);
字符串前半部分=转换字(numReversed,actnumber);
整数tmp=Math.round(junkValPaisa*100);
junkValPaisa=(十进制)tmp/100;System.debug('jj:'+junkValPaisa);
字符串paisaStr=junkValPaisa.toPlainString();
串后半部分;
如果(paisaStr==“0”){
后半部分=“”;
}else if(paisaStr.length()!=4){
paisaStr=paisaStr+'0';
paisaStr=paisaStr.子串(2);
字符串[]numReversedPaisa=paisaStr.split(“”);
字符串[]actnumberPaisa=反向(numReversedPaisa);
secondHalf=转换字(numReversedPaisa,actnumberPaisa);
}否则{
paisaStr=paisaStr.子串(2);
字符串[]numReversedPaisa=paisaStr.split(“”);
字符串[]actnumberPaisa=反向(numReversedPaisa);
secondHalf=转换字(numReversedPaisa,actnumberPaisa);
}
字符串SumOFHalves='';
如果(secondHalf.length()>4){
firstHalf=firstHalf.替换为('Only','Rupess And');
secondHalf=secondHalf.replace('Only','Paisa Only');
SumOFHalves=上半部分+下半部分;
}否则{
firstHalf=firstHalf.replace('Only','Rupess Only');
SumOFHalves=上半部分;
}
//如果金额有任何价值
if(SumOFHalves.length()>5){
返回阀门;
}否则{
返回“”;
}
}
//方法倒数
公共列表反向(列表strToRev){
List revList=新列表();
对于(整数i=strtorrev.size()-1;i>=0;i--){
revList.add(strtorrev.get(i));
}
revList.add(“”);
返回列表;
}
公共字符串convertInWords(字符串[]numRev,字符串[]actnum){
列表iWords=新列表{'0','1','2','3','4','5','6','7','8','9'};
List ePlace=新列表{'10','11','12','13','14','15','16','17','18','19'};
List tensPlace=新列表{'dummy','Ten','two','threen','threen','50','threen','threen','threen','threen','threen','threen','threen','threen','threen',';
整数iWordsLength=numRev.size();
字符串totalWords='';
列表inWords=新列表();
for(整数k=0;k0){
大写字母[j]=大写字母[j]+‘千’;
}
}else如果(i==4){
如果(actnum[i]=“0”){
换句话说[j]='';
}else if(actnum[i]=“1”){
换句话说[j]=ePlace[Integer.valueof(actnum[i-1]);
}否则{
换句话说[j]=tensPlace[Integer.valueof(actnum[i]);
}
}else如果(i==5){
如果(actnum[i]='0'| | actnum[i+1]=='1'){
换句话说[j]='';
}否则{
inWords[j]=iWords[Integer.valueof(actnum[i]);
}
if(actnum[i+1]!=“0”| Integer.valueof(actnum[i])>0){
in words[j]=in words[j]+'Lakh';
}
}else如果(i==6){
如果(actnum[i]=“0”){
换句话说[j]='';
}else if(actnum[i]=“1”){
换句话说[j]=ePlace[Integer.valueof(actnum[i-1]);
}否则{
换句话说[j]=tensPlace[Integer.valueof(actnum[i]);
}
}else如果(i==7){
if(actnum[i
String.valueOf(num);