Monday, May 23, 2016

Convert Number To Words

Recalling This Codeunit Purpose ... One of my Most Respected Sir Asked Me Can You Describe the Overview How Would You Represent/Convert the  Number into Words. During My Initial Coding Days i have Done This. Just wanted To post/share So that it might be helpful for other saves times.

So Lets Start , Create a New Code Unit (it is not required to create new But to start Fresh) and Create Two Functions Like Below


Select the Second Function i.e Num2Word in Pic And Select Locals to define local variables And Parameters required.
Take Amount as parameter of type integer or you might use Decimal . i havent thought about it at that time..

take Local variables as above...
Define Return type as Text to function

Define text constants 
Define a Global variable that is word which is used across functions and it is Array of 28 so declare the dimensions as 28
Now lets get into code, 



So first i have declare all words that might possibly required have assigned into Array. Then i found the no of digits it got for a number/Amount.
Yes i forgot to say it Will convert number to  in millions i.e divisible by 1000s 
Example :
Input = 1000000 
Output = One Million


For suppose if you want To Convert into INR(Indian Currency) then make some Conditions Or Replace While Loop With Below Code

So then i will start a while loop will continue until number is Zero .
In loop i'm send remainder that of 1000 i.e Ones , tens and hundreds places then followed by thousands places followed by Millions  The Convertion Is done in another function which is recursive /called multiple times .

Lets get into another function...
Select Another Function and Start declaring its Locals variables, parameter and return value like below


This is Parameter which we use So make it variable by Checking var box to left
Return Value be text Length might be exception I think 
These are the variables  retword is nothing but word which we would return dig1,dig2,dig3 are digits 
respectively word1 ,word2 , word3 ones , tens and hundre...s  Arraynum is to Get the respective Word From word array which consists of 27 text words...  which we defined in previous Function i.e Num2Word.

So getting into code



First it finds 3rd digit i.e hundreds  by dividing num/100 and gets Respective word by Words[dig3]
then adds a Hundreds text at last. 

Then Value /Number is reduced by 1 digit By using Mod 100 So that if number is Less than 20 Or Greater then accordingly it retrieves Words...
Thus Returns A Final Word  to Num2Word fuction..

I hope you guys Understood Let me know if you Have any Doubts /found any mistakes .

If you Want to try my Objects Download From HERE

Thank You.:)

No comments:

Post a Comment