function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Geetha ReddyGeetha Reddy 

Urgent Issue Converting number to Word......

How can i convert number to word11

for eg: if i give Input : 1265

                         output: twelve hundred sixty five only.

 

i have tried this in javascript, its working fine , but unable to pass javascript value to custom object field value.

 

<HTML>
 <HEAD>
 <script type = "text/javascript">
function test_skill() {
var junkVal="1000" // 
//var junkVal=document.getElementById('rupees').value;
junkVal=Math.floor(junkVal);
var obStr=new String(junkVal);
numReversed=obStr.split("");
actnumber=numReversed.reverse();
if(Number(junkVal) >=0){
//do nothing
}
else{
alert('wrong Number cannot be converted');
return false;
}
if(Number(junkVal)==0){
document.getElementById('container').innerHTML=obStr+''+'Rupees Zero Only';
return false;
}
if(actnumber.length>9){
alert('Oops!!!! the Number is too big to covertes');
return false;
}
var iWords=["Zero", " One", " Two", " Three", " Four", " Five", " Six", " Seven", " Eight", " Nine"];
var ePlace=['Ten', ' Eleven', ' Twelve', ' Thirteen', ' Fourteen', ' Fifteen', ' Sixteen', ' Seventeen', ' Eighteen', ' Nineteen'];
var tensPlace=['dummy', ' Ten', ' Twenty', ' Thirty', ' Forty', ' Fifty', ' Sixty', ' Seventy', ' Eighty', ' Ninety' ];
var iWordsLength=numReversed.length;
var totalWords="";
var inWords=new Array();
var finalWord="";
j=0;
for(i=0; i<iWordsLength; i++){
switch(i)
{
case 0:
if(actnumber[i]==0 || actnumber[i+1]==1 ) {
inWords[j]='';
}
else {
inWords[j]=iWords[actnumber[i]];
}
inWords[j]=inWords[j]+' Only';
break;
case 1:
tens_complication();
break;
case 2:
if(actnumber[i]==0) {
inWords[j]='';
}
else if(actnumber[i-1]!=0 && actnumber[i-2]!=0) {
inWords[j]=iWords[actnumber[i]]+' Hundred and';
}
else {
inWords[j]=iWords[actnumber[i]]+' Hundred';
}
break;
case 3:
if(actnumber[i]==0 || actnumber[i+1]==1) {
inWords[j]='';
}
else {
inWords[j]=iWords[actnumber[i]];
}
if(actnumber[i+1] != 0 || actnumber[i] > 0){
inWords[j]=inWords[j]+" Thousand";
}
break;
case 4:
tens_complication();
break;
case 5:
if(actnumber[i]==0 || actnumber[i+1]==1) {
inWords[j]='';
}
else {
inWords[j]=iWords[actnumber[i]];
}
if(actnumber[i+1] != 0 || actnumber[i] > 0){
inWords[j]=inWords[j]+" Lakh";
}
break;
case 6:
tens_complication();
break;
case 7:
if(actnumber[i]==0 || actnumber[i+1]==1 ){
inWords[j]='';
}
else {
inWords[j]=iWords[actnumber[i]];
}
inWords[j]=inWords[j]+" Crore";
break;
case 8:
tens_complication();
break;
default:
break;
}
j++;
}
function tens_complication() {
if(actnumber[i]==0) {
inWords[j]='';
}
else if(actnumber[i]==1) {
inWords[j]=ePlace[actnumber[i-1]];
}
else {
inWords[j]=tensPlace[actnumber[i]];
}
}
inWords.reverse();
for(i=0; i<inWords.length; i++) {
finalWord+=inWords[i];
}
document.getElementById('container').innerHTML=finalWord; // we are getting values in to it.
}
 </script>
 </HEAD>
 <BODY onload="test_skill()">
 
<div id="container"></div>
 </BODY>
 
</HTML>
i want that value (document.getElementById('container').innerHTML=finalWord;) in field namely amountinwords__c .
bob_buzzardbob_buzzard

You should be able to do this if you have a form in the page with an input backed by that field.  You can access the underlying input element using the $Component global.

Geetha ReddyGeetha Reddy

but value is coming from javascript na

 

so I have a doubt ... 

bob_buzzardbob_buzzard

What do you mean by coming from javascript na?  Does that mean that your javascript is returning the string 'na'?

Geetha ReddyGeetha Reddy

no actually i am redering a pdf 

 

in it i have a field namely TotalPrice from that all procedure are done in javascript after that value is stored in finalword .

 

Now i want final word value to amountinwords__c .

bob_buzzardbob_buzzard

PDF can't use javascript - that's in the visualforce developers guide.  You'll need to push that code server side.  I'd imagine you'll need a custom controller with a getter that returns the field in question converted.

Geetha ReddyGeetha Reddy

ya i too know that we can not use javascript while generating pdf .. 

 

but i want to convert number to word so i tried to write in visualforce page .

 

bob_buzzardbob_buzzard

I'm confused as to what you are looking for now.  You can't use javascript, so it will have to be done in Apex and the correct value rendered through a component.

Pranav MarathePranav Marathe

I'm also looking for a similar solution but my requirements are little different. It's a professional edition hence there is no apex available.

 

I want opportunity amount to be converted into words. Doesn't matter even if it is done with a click of button. I have created a custom button in opportunity. On clicking this button, javascript will be executed to convert number into word. I am facing similar problem what Geetha faced.. "unable to pass javascript value to custom object field value"

 

I referred this code initially and I can pass a text value directly in Next Step field.

 

{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")} 
 
var newRecords = [];
 
var c = new sforce.SObject("Opportunity"); 
c.id ="{!Opportunity.Id}";
c.NextStep = 'Test";
newRecords.push(c); 
 
result = sforce.connection.update(newRecords); 
 
window.location.reload();

 I tried to use same logic to pass a value calculated from javascript function but it's not working...

 

{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")}

var newRecords = [];

var c = new sforce.SObject("Opportunity");
c.id ="{!Opportunity.Id}";


function Convert() {

var rVal=c.amount;
rVal=Math.floor(rVal);
var rup=new String(rVal);
rupRev=rup.split("");
actualNumber=rupRev.reverse();

if(Number(rVal) >=0){

}
else{
alert('Number cannot be converted');
return false;
}
if(Number(rVal)==0){
c.nextstep=rup+''+'Rupees Zero Only';
return false;
}
if(actualNumber.length>9){
alert('the Number is too big to covertes');
return false;
}

var numWords=["Zero", " One", " Two", " Three", " Four", " Five", " Six", " Seven", " Eight", " Nine"];
var numPlace=['Ten', ' Eleven', ' Twelve', ' Thirteen', ' Fourteen', ' Fifteen', ' Sixteen', ' Seventeen', ' Eighteen', ' Nineteen'];
var tPlace=['dummy', ' Ten', ' Twenty', ' Thirty', ' Forty', ' Fifty', ' Sixty', ' Seventy', ' Eighty', ' Ninety' ];

var numWordsLength=rupRev.length;
var totalWords="";
var numtoWords=new Array();
var finalWord="";
j=0;
for(i=0; i<numWordsLength; i++){
switch(i)
{
case 0:
if(actualNumber[i]==0 || actualNumber[i+1]==1 ) {
numtoWords[j]='';
}
else {
numtoWords[j]=numWords[actualNumber[i]];
}
numtoWords[j]=numtoWords[j]+' Only';
break;
case 1:
CTen();
break;
case 2:
if(actualNumber[i]==0) {
numtoWords[j]='';
}
else if(actualNumber[i-1]!=0 && actualNumber[i-2]!=0) {
numtoWords[j]=numWords[actualNumber[i]]+' Hundred and';
}
else {
numtoWords[j]=numWords[actualNumber[i]]+' Hundred';
}
break;
case 3:
if(actualNumber[i]==0 || actualNumber[i+1]==1) {
numtoWords[j]='';
}
else {
numtoWords[j]=numWords[actualNumber[i]];
}
if(actualNumber[i+1] != 0 || actualNumber[i] > 0){
numtoWords[j]=numtoWords[j]+" Thousand";
}
break;
case 4:
CTen();
break;
case 5:
if(actualNumber[i]==0 || actualNumber[i+1]==1) {
numtoWords[j]='';
}
else {
numtoWords[j]=numWords[actualNumber[i]];
}
if(actualNumber[i+1] != 0 || actualNumber[i] > 0){
numtoWords[j]=numtoWords[j]+" Lakh";
}
break;
case 6:
CTen();
break;
case 7:
if(actualNumber[i]==0 || actualNumber[i+1]==1 ){
numtoWords[j]='';
}
else {
numtoWords[j]=numWords[actualNumber[i]];
}
numtoWords[j]=numtoWords[j]+" Crore";
break;
case 8:
CTen();
break;
default:
break;
}
j++;
}

function CTen() {
if(actualNumber[i]==0) {
numtoWords[j]='';
}
else if(actualNumber[i]==1) {
numtoWords[j]=numPlace[actualNumber[i-1]];
}
else {
numtoWords[j]=tPlace[actualNumber[i]];
}
}
numtoWords.reverse();
for(i=0; i<numtoWords.length; i++) {
finalWord+=numtoWords[i];
}
c.netxstep=rup+' '+finalWord;
}

newRecords.push(c);
result = sforce.connection.update(newRecords);

window.location.reload();

 Am I missing any basic step?

 

Pranav MarathePranav Marathe

Here is the working sample of Currency to Words using Custom Button and Javascript.

 

Custom Text field:

Click on Your Name > Setup > App Setup > Customize > Opportunities > Fields

Click on new button and create a custom "Text" field.

Name it as "amount in words".

Add it on Page Layout.

 

Custom Button:

Click on Your Name > Setup > App Setup > Customize > Opportunities > Buttons and Links

Click new button to create a custom button. Name it as "Currency to Words"

Set Display Type as "Detail Page Button"

Set Behavior as "Execute JavaScript".

Paste the below mentioned code in "OnClick JavaScript" & Save.

Add this button on opportunity page layout

 

 

{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")} 

var newRecords = []; 

var c = new sforce.SObject("Opportunity"); 
c.id ="{!Opportunity.Id}"; 
c.amount ="{!text(Opportunity.Amount)}"; 

var junkVal=c.amount; 
junkVal=Math.floor(junkVal); 
var obStr=new String(junkVal); 
numReversed=obStr.split(""); 
actnumber=numReversed.reverse(); 

if(Number(junkVal) >=0){ 
//do nothing 
} 
else{ 
alert('wrong Number cannot be converted'); 
return false; 
} 
if(Number(junkVal)==0){ 
document.getElementById('container').innerHTML=obStr+''+'Rupees Zero Only'; 
return false; 
} 
if(actnumber.length>9){ 
alert('Oops!!!! the Number is too big to covertes'); 
return false; 
} 

var iWords=["Zero", " One", " Two", " Three", " Four", " Five", " Six", " Seven", " Eight", " Nine"]; 
var ePlace=['Ten', ' Eleven', ' Twelve', ' Thirteen', ' Fourteen', ' Fifteen', ' Sixteen', ' Seventeen', ' Eighteen', ' Nineteen']; 
var tensPlace=['dummy', ' Ten', ' Twenty', ' Thirty', ' Forty', ' Fifty', ' Sixty', ' Seventy', ' Eighty', ' Ninety' ]; 

var iWordsLength=numReversed.length; 
var totalWords=""; 
var inWords=new Array(); 
var finalWord=""; 
j=0; 
for(i=0; i<iWordsLength; i++){ 
switch(i) 
{ 
case 0: 
if(actnumber[i]==0 || actnumber[i+1]==1 ) { 
inWords[j]=''; 
} 
else { 
inWords[j]=iWords[actnumber[i]]; 
} 
inWords[j]=inWords[j]+' Only'; 
break; 
case 1: 
tens_complication(); 
break; 
case 2: 
if(actnumber[i]==0) { 
inWords[j]=''; 
} 
else if(actnumber[i-1]!=0 && actnumber[i-2]!=0) { 
inWords[j]=iWords[actnumber[i]]+' Hundred and'; 
} 
else { 
inWords[j]=iWords[actnumber[i]]+' Hundred'; 
} 
break; 
case 3: 
if(actnumber[i]==0 || actnumber[i+1]==1) { 
inWords[j]=''; 
} 
else { 
inWords[j]=iWords[actnumber[i]]; 
} 
if(actnumber[i+1] != 0 || actnumber[i] > 0){ 
inWords[j]=inWords[j]+" Thousand"; 
} 
break; 
case 4: 
tens_complication(); 
break; 
case 5: 
if(actnumber[i]==0 || actnumber[i+1]==1) { 
inWords[j]=''; 
} 
else { 
inWords[j]=iWords[actnumber[i]]; 
} 
if(actnumber[i+1] != 0 || actnumber[i] > 0){ 
inWords[j]=inWords[j]+" Lac"; 
} 
break; 
case 6: 
tens_complication(); 
break; 
case 7: 
if(actnumber[i]==0 || actnumber[i+1]==1 ){ 
inWords[j]=''; 
} 
else { 
inWords[j]=iWords[actnumber[i]]; 
} 
inWords[j]=inWords[j]+" Crore"; 
break; 
case 8: 
tens_complication(); 
break; 
default: 
break; 
} 
j++; 
} 

function tens_complication() { 
if(actnumber[i]==0) { 
inWords[j]=''; 
} 
else if(actnumber[i]==1) { 
inWords[j]=ePlace[actnumber[i-1]]; 
} 
else { 
inWords[j]=tensPlace[actnumber[i]]; 
} 
} 
inWords.reverse(); 
for(i=0; i<inWords.length; i++) { 
finalWord+=inWords[i]; 
} 

c.amount_in_words__c=finalWord; 

newRecords.push(c); 
result = sforce.connection.update(newRecords); 

window.location.reload();

 

Hope this will help... Next challenge is to create this as a dynamic functionality. Currently there is no control to check whether currency the text and opportunity amount is correct or wrong.

Michael ParksMichael Parks
This has been working really well for me, but now suddenly when I click my button to execute this code I get an Illegal Return Statement error. Now even when I enter the above example into a button as-is I still get the same error, and I know it worked fine initially when I started out using it a few months ago. Does anyone know if something has changed in Salesforce to prevent this code from running now? 
Pranav MarathePranav Marathe
It's still working well in my instance..
Krrish GopalKrrish Gopal
Hi Pranav,

Here I want that Currency in Words button also cover paise in words.

like if an amount is 110.12 rupees then it shows One Hundred Ten Rupee and 12 Paise Only
Rajesh3699Rajesh3699
Hi All, 
The below code works for the number range 1000 - 99,99,999
Just set the respective value for the num. variable. [ you can make the code as a method also and can be called where ever necessary]

Hope this helps someone.

Public Class AmountInWords_New{
    
        Long num = 45678;
        Integer k, ends;
        long a ,b, c, d, f, g;
        public Map<Integer,String> wordMap = new Map<Integer, String>();
        public List<String> inWords = new List<String>();
        public string amountinWords {get;set;}
        
       
        
   public  AmountInWords_New(){
       
       amountInWords = 'Amount in words';
       //Load the map starts here
            wordMap.put(99, 'Ninty Nine');
        wordMap.put(98, 'Ninty Eight');
        wordMap.put(97, 'Ninty Seven');
        wordMap.put(96, 'Ninty Six');
        wordMap.put(95, 'Ninty Five');
        wordMap.put(94, 'Ninty Four');
        wordMap.put(93, 'Ninty Three');
        wordMap.put(92, 'Ninty Two');
        wordMap.put(91, 'Ninty One');
        wordMap.put(90, 'Ninty');
        wordMap.put(89, 'Eighty Nine');
        
        wordMap.put(88, 'Eighty Eight');
        wordMap.put(87, 'Eighty Seven');
        wordMap.put(86, 'Eighty Six');
        wordMap.put(85, 'Eighty Five');
        wordMap.put(84, 'Eighty Four');
        wordMap.put(83, 'Eighty Three');
        wordMap.put(82, 'Eighty Two');
        wordMap.put(81, 'Eighty One');
        wordMap.put(80, 'Eighty');
        wordMap.put(79, 'Seventy Nine');
        wordMap.put(78, 'Seventy Eight');
        wordMap.put(77, 'Seventy Seven');
        wordMap.put(76, 'Seventy Six');
        wordMap.put(75, 'Seventy Five');
        wordMap.put(74, 'Seventy Four');
        wordMap.put(73, 'Seventy Three');
        wordMap.put(72, 'Seventy Two');
        wordMap.put(71, 'Seventy One');
        wordMap.put(70, 'Seventy');
        wordMap.put(69, 'Sixty Nine');
        wordMap.put(68, 'Sixty Eight');
        wordMap.put(67, 'Sixty Seven');
        
        
        wordMap.put(66, 'Sixty Six');
        wordMap.put(65, 'Sixty Five');
        wordMap.put(64, 'Sixty Four');
        wordMap.put(63, 'Sixty Three');
        wordMap.put(62, 'Sixty Two');
        wordMap.put(61, 'Sixty One');
        wordMap.put(60, 'Sixty');
        wordMap.put(59, 'Fifty Nine');
        wordMap.put(58, 'Fifty Eight');
        wordMap.put(57, 'Fifty Seven');
        wordMap.put(56, 'Fifty Six');
        
        wordMap.put(55, 'Fifty Five');
        wordMap.put(54, 'Fifty Four');
        wordMap.put(53, 'Fifty Three');
        wordMap.put(52, 'Fifty Two');
        wordMap.put(51, 'Fifty One');
        wordMap.put(50, 'Fifty');
        wordMap.put(49, 'Fourty Nine');
        wordMap.put(48, 'Fourty Eight');
        wordMap.put(47, 'Fourty Seven');
        wordMap.put(46, 'Fourty Six');
        wordMap.put(45, 'Fourty Five');
        
        wordMap.put(44, 'Fourty Four');
        wordMap.put(43, 'Fourty Three');
        wordMap.put(42, 'Fourty Two');
        wordMap.put(41, 'Fourty One');
        wordMap.put(40, 'Fourty');
        wordMap.put(39, 'Thirty Nine');
        wordMap.put(38, 'Thirty Eight');
        wordMap.put(37, 'Thirty Seven');
        wordMap.put(36, 'Thirty Six');
        wordMap.put(35, 'Thirty Five');
        wordMap.put(34, 'Thirty Four');
        
        wordMap.put(33, 'Thirty Three');
        wordMap.put(32, 'Thirty Two');
        wordMap.put(31, 'Thirty One');
        wordMap.put(30, 'Thirty');
        wordMap.put(29, 'Twenty Nine');
        wordMap.put(28, 'Twenty Eight');
        wordMap.put(27, 'Twenty Seven');
        wordMap.put(26, 'Twenty Six');
        wordMap.put(25, 'Twenty Five');
        wordMap.put(24, 'Twenty Four');
        wordMap.put(23, 'Twenty Three');
        
        wordMap.put(22, 'Twenty Two');
        wordMap.put(21, 'Twenty One');
        wordMap.put(20, 'Twenty');
        wordMap.put(19, 'Ninteen');
        wordMap.put(18, 'Eighteen');
        wordMap.put(17, 'Seventeen');
        wordMap.put(16, 'Sixtenn');
        wordMap.put(15, 'Fifteen');
        wordMap.put(14, 'Fourteen');
        wordMap.put(13, 'thirteen');
        wordMap.put(12, 'Twelve');
        
        wordMap.put(11, 'Eleven');
        wordMap.put(10, 'Ten');
        wordMap.put(9, 'Nine');
        wordMap.put(8, 'Eight');
        wordMap.put(7, 'Seven');
        wordMap.put(6, 'Six');
        wordMap.put(5, 'Five');
        wordMap.put(4, 'Four');
        wordMap.put(3, 'Three');
        wordMap.put(2, 'Two');
        wordMap.put(1, 'One');
    
    //Load the map ends here


        
        While (ends != 0){
            
            //Calculate the length of the AmountInWords
            if( num >= 9999999){
                k = 2;
            }  
            else 
            k = 1;
            String nu = num.format();
            system.debug('the value is' + nu);
            Integer len = nu.length();
            len= len - k;
            
            system.debug('the length is' + len);
            system.debug('the number is' + num);
            //Scenario 10,00,000 or 55,66,777
            if(len >= 6){
                system.debug('inside the 1st loop');
                a = num / 100000;
                system.debug('the value is a' + a); 
                
                //check mod is 0 or not
                b = Math.mod(num , 100000);
                //for scenario 55,00,000;
                if(b == 0){
                     inWords.add(wordMap.get(a.intValue()));
                     inWords.add('Lakhs');
                     ends = 0;
                }
                //for scenario 55,00,057
                else if(b < 100){
                     system.debug('word is' + wordMap.get(a.intValue()));
                     inWords.add(wordMap.get(a.intValue()));
                     inWords.add('Lakhs');
                     inWords.add(wordMap.get(b.intValue()));
                     ends = 0;
                }
                else{
                    inWords.add(wordMap.get(a.intValue()));
                    inWords.add('Lakhs');
                    num = b;
                    system.debug('some other scenario mod >=100');   
                }
            }
            
            //Scenario 99,789 or 9,789
            else if(len == 5 || len == 4){
            system.debug('inside the 2nd loop');
                c = num / 1000;
                
                //check mod is 0 or not
                d = Math.mod(num , 1000);
                //for scenario 55,000 or 5000
                if(d == 0){
                     inWords.add(wordMap.get(c.intValue()));
                     inWords.add('Thousand');
                     ends = 0;
                }
                //for scenario 55,057 or 5,057
                else if(d < 100){
                     inWords.add(wordMap.get(c.intValue()));
                     inWords.add('Thousand');
                     inWords.add(wordMap.get(d.intValue()));
                     ends = 0;
                }
                //for scenario 55,678 or 5,987
                else{
                    inWords.add(wordMap.get(c.intValue()));
                     inWords.add('Thousand');
                    num = d;
                    system.debug('some other scenario mod >=100');   
                }
            }
            else {
                system.debug('inside the 3rd loop');
                f = num / 100;
                
                //check mod is 0 or not
                g = Math.mod(num , 100);
                if(g == 0){
                     inWords.add(wordMap.get(f.intValue()));
                     inWords.add('Hundred');
                     ends = 0;
                }
                else if(g < 100){
                     inWords.add(wordMap.get(f.intValue()));
                     inWords.add('Hundred');
                     inWords.add(wordMap.get(g.intValue()));
                     ends = 0;
                }
                else{
                    num = g;
                    system.debug('some other scenario mod >=100 ');   
                }
            
            }
        
        
        }
        
        inWords.add('only');
        inWords.add('\n');
        system.debug('the list is' + inWords);
        for(Integer z = 0;z<inWords.size()-1;z++){
            amountInWords = amountInWords + ' ' + string.ValueOf(inWords[z]);
            
        }
        system.debug('in words is:' + amountInWords);

    }

}

Thank You,
Adiga,
Hosanagara.