OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

NumToWords convert 423 to Four Hundred Twenty Three
Goto page 1, 2, 3  Next
 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets
View previous topic :: View next topic  
Author Message
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Sun Feb 15, 2004 7:39 am    Post subject: NumToWords convert 423 to Four Hundred Twenty Three Reply with quote

Here is a silly little function to convert numbers into words text.

This handles negative numbers, decimals, and numbers up to 999 trillion.

I was planning to eventually include this routine as part of Danny's Writer Power Tools, but someone requested this function, so I'm posting it.

It is the same function I've re-written over and over again for years, just translated to OOo Basic.

The function is recursive. (That is, it calls itself.)

Code:
Sub Main
   Print NumToWords( -115628400.23 )
End Sub


Function NumToWords( ByVal nNumber As Double ) As String
   cWords = ""
   
   If nNumber < 0 Then
      cWords = "Negative " + NumToWords( -1 * nNumber )
   
   ElseIf nNumber <> Int( nNumber ) Then
      cWords = NumToWords( Int( nNumber ) )
      cWords = cWords + " point"
      nFrac = nNumber - Int( nNumber )
      Do
         nFrac = nFrac * 10
         nDigit = Int( nFrac )
         nFrac = nFrac - nDigit
         
         If nDigit > 0 Then
            cDigitWord = NumToWords( nDigit )
            cWords = cWords + " " + cDigitWord
         Else
            Exit Do
         EndIf
      Loop
   
   ElseIf nNumber < 20 Then
      Select Case nNumber
         Case 0: cWords = "Zero"
         Case 1: cWords = "One"
         Case 2: cWords = "Two"
         Case 3: cWords = "Three"
         Case 4: cWords = "Four"
         Case 5: cWords = "Five"
         Case 6: cWords = "Six"
         Case 7: cWords = "Seven"
         Case 8: cWords = "Eight"
         Case 9: cWords = "Nine"
         Case 10: cWords = "Ten"
         Case 11: cWords = "Eleven"
         Case 12: cWords = "Twelve"
         Case 13: cWords = "Thirteen"
         Case 14: cWords = "Fourteen"
         Case 15: cWords = "Fifteen"
         Case 16: cWords = "Sixteen"
         Case 17: cWords = "Seventeen"
         Case 18: cWords = "Eighteen"
         Case 19: cWords = "Ninteen"
      End Select
   
   ElseIf nNumber < 100 Then
      nTensPlace = Int( nNumber / 10 )
      nOnesPlace = nNumber Mod 10
      Select Case nTensPlace * 10
         Case 20: cWords = "Twenty"
         Case 30: cWords = "Thirty"
         Case 40: cWords = "Forty"
         Case 50: cWords = "Fifty"
         Case 60: cWords = "Sixty"
         Case 70: cWords = "Seventy"
         Case 80: cWords = "Eighty"
         Case 90: cWords = "Ninty"
      End Select
      If nOnesPlace > 0 Then
         cWords = cWords + " " + NumToWords( nOnesPlace )
      EndIf
   
   ElseIf nNumber < 1000 Then
      nHundredsPlace = Int( nNumber / 100 )
      nRest = nNumber Mod 100
      cWords = NumToWords( nHundredsPlace )
      cWords = cWords + " Hundred"
      If nRest > 0 Then
         cWords = cWords + " " + NumToWords( nRest )
      EndIf
   
   ElseIf nNumber < 1000000 Then
      nThousands = Int( nNumber / 1000 )
      nRest = nNumber Mod 1000
      cWords = NumToWords( nThousands )
      cWords = cWords + " Thousand"
      If nRest > 0 Then
         cWords = cWords + " " + NumToWords( nRest )
      EndIf
   
   ElseIf nNumber < 1000000000 Then
      nMillions = Int( nNumber / 1000000 )
      nRest = Int( nNumber Mod 1000000 )
      cWords = NumToWords( nMillions )
      cWords = cWords + " Million"
      If nRest > 0 Then
         cWords = cWords + " " + NumToWords( nRest )
      EndIf
   
   ElseIf nNumber < 1000000000000 Then
      nBillions = Int( nNumber / 1000000000 )
      nRest = Int( nNumber Mod 1000000000 )
      cWords = NumToWords( nBillions )
      cWords = cWords + " Billion"
      If nRest > 0 Then
         cWords = cWords + " " + NumToWords( nRest )
      EndIf
   
   ElseIf nNumber < 1000000000000000 Then
      nTrillions = Int( nNumber / 1000000000000 )
      nRest = Int( nNumber Mod 1000000000000 )
      cWords = NumToWords( nTrillions )
      cWords = cWords + " Trillion"
      If nRest > 0 Then
         cWords = cWords + " " + NumToWords( nRest )
      EndIf
      
      ' You can follow the pattern of the Millions / Billions / Trillions
      '  if you need bigger numbers.
      
   EndIf
   
   NumToWords() = cWords
End Function


_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
cwchia
Super User
Super User


Joined: 09 Jan 2003
Posts: 1050
Location: Malaysia

PostPosted: Mon Feb 16, 2004 7:33 pm    Post subject: Re: NumToWords convert 423 to Four Hundred Twenty Three Reply with quote

DannyB wrote:
I was planning to eventually include this routine as part of Danny's Writer Power Tools, but someone requested this function, so I'm posting it.


LUV LUV LUV and Thanks !!!!!!!!!!!!!!!!!!! Razz

I made a request earlier in this forum but probably slip your eyes Wink

---------------------------------------

After I copy the macro codes into my OOo, I found that I have a problem, I do not know how to invoke the macro ! Embarassed Embarassed Embarassed (The number that I intend to convert to word is within a cell in a table)

Just to add, I knwo how to invoke the macro in a calc sheet by this '=numtowords(423.23)' but is there a way to accoplish the same task in writer?

Also can anyone please point to me which part to modify so that the macro convert to words like one writing a cheque eg Four Hundred twenty Three and Cents Twenty Three
Back to top
View user's profile Send private message
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Tue Feb 17, 2004 6:00 am    Post subject: Reply with quote

cwchia wrote:
I made a request earlier in this forum but probably slip your eyes

Oh, sorry. Embarassed


cwchia wrote:
After I copy the macro codes into my OOo, I found that I have a problem, I do not know how to invoke the macro ! (The number that I intend to convert to word is within a cell in a table)

Just to add, I knwo how to invoke the macro in a calc sheet by this '=numtowords(423.23)' but is there a way to accoplish the same task in writer?

Does a formula not work in a table in Writer? Maybe there is a way to insert an actual portion of a Calc spreadsheet in writer and make it look like a table?

Maybe there is some way to call a macro function from within Writer in general? (Wouldn't that be cold!) There might be some kind of specialized "field" in Writer that can call a macro function?


cwchia wrote:
Also can anyone please point to me which part to modify so that the macro convert to words like one writing a cheque eg Four Hundred twenty Three and Cents Twenty Three

Try this...
Code:
Sub Main
   Print NumToDollars( 4234.07 )
End Sub


Function NumToDollars( ByVal nNumber As Double ) As String
   cWords = ""
   
   nDollars = Int( nNumber )
   nCents = Int( (nNumber - Int( nNumber ) + 0.005) * 100 )
   
   cDollars = NumToWords( nDollars )
   cCents = CSTR( nCents )
   If Len( cCents ) < 2 Then
      cCents = "0" & cCents
   EndIf
   
   cWords = cDollars & " Dollars and " & cCents & " Cents"
   
   NumToDollars() = cWords
End Function

_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
avantman42
Super User
Super User


Joined: 28 Jul 2003
Posts: 751
Location: Staffordshire, UK

PostPosted: Tue Feb 17, 2004 6:09 am    Post subject: Reply with quote

DannyB wrote:
Does a formula not work in a table in Writer? Maybe there is a way to insert an actual portion of a Calc spreadsheet in writer and make it look like a table?

Maybe there is some way to call a macro function from within Writer in general?


I have got functions to work in Writer. I can't remember the exact steps, but I think the function was within the soffice Standard library, then I just used F2 to display the function toolbar, and entered the function.

Russ
Back to top
View user's profile Send private message
cwchia
Super User
Super User


Joined: 09 Jan 2003
Posts: 1050
Location: Malaysia

PostPosted: Tue Feb 17, 2004 9:13 pm    Post subject: Reply with quote

avantman42 wrote:
I have got functions to work in Writer. I can't remember the exact steps, but I think the function was within the soffice Standard library, then I just used F2 to display the function toolbar, and entered the function.

If that is so does it mean that I have to put the macro code into the Standard library?
Back to top
View user's profile Send private message
avantman42
Super User
Super User


Joined: 28 Jul 2003
Posts: 751
Location: Staffordshire, UK

PostPosted: Tue Feb 17, 2004 11:41 pm    Post subject: Reply with quote

cwchia wrote:
avantman42 wrote:
I have got functions to work in Writer. I can't remember the exact steps, but I think the function was within the soffice Standard library, then I just used F2 to display the function toolbar, and entered the function.

If that is so does it mean that I have to put the macro code into the Standard library?


Yes, but I've just tried to do it again, and I can't seem to make it work, so I think I may have been wrong.

Sorry Embarassed

Russ
Back to top
View user's profile Send private message
cwchia
Super User
Super User


Joined: 09 Jan 2003
Posts: 1050
Location: Malaysia

PostPosted: Wed Feb 18, 2004 8:07 pm    Post subject: Reply with quote

Hi Russ,
Just reliased that I always put macros under the standard library in my OOo so that they are available to me any time. Still cannot find a way to get it working in writer. Mad

Hi Danny,

I find that numtowords cannot handle decimals of more them 3, it give strange results like 12.3456 as Twelve point three four five five nine nine nine ...

whereas numtodollars failed to convert negative amount. negative$12.40 becomes Negative Thirteen Dollars and 60 cents.

I'm trying to change that part of the ## cents to words as well so that the end result will be like this RM10.35 --> Ringgit Malaysia Ten and Sen Thirty Five Only (this is how we write currency in Malaysia). Adding a string like "Ringgit Malaysia " in front of cDollars in the last line of cWord = cDollors .... always yield an error. Will do some reading now.
Back to top
View user's profile Send private message
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Thu Feb 19, 2004 7:28 am    Post subject: Reply with quote

cwchia wrote:
I find that numtowords cannot handle decimals of more them 3, it give strange results like 12.3456 as Twelve point three four five five nine nine nine ...

whereas numtodollars failed to convert negative amount. negative$12.40 becomes Negative Thirteen Dollars and 60 cents.


Notice how in NumToDollars, I added the 0.005 to do 5/4 rounding in the last digit.

I suspect that OOo's math does not have any guard digits. If you take 12.3456, then subtract 12, leaving 0.3456, then multiply by 10, to get 3.456, then remove 3, then multiply by 10, to get 4.56, at some point, it becomes 4.55999999...

If you want a specific number of decimal places, you can do the 5/4 rounding trick like I did in NumToDollars().
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
cwchia
Super User
Super User


Joined: 09 Jan 2003
Posts: 1050
Location: Malaysia

PostPosted: Thu Feb 19, 2004 10:01 pm    Post subject: Reply with quote

As my table is formatted to take only 2 decimal points, I'll modify that part of your code as well. Thanks.
Back to top
View user's profile Send private message
ryck
Newbie
Newbie


Joined: 02 Dec 2004
Posts: 1
Location: Mty, MX

PostPosted: Thu Dec 02, 2004 9:08 pm    Post subject: Mexican pesos (spanish version) Reply with quote

For those who would like the spanish version for ie mexican pesos, here's my NumToPesos Contrib:

Sub Main
Print NumToWords( -115628400.23 )
End Sub


Function NumToWords( ByVal nNumber As Double ) As String
cWords = ""

If nNumber < 0 Then
cWords = "Negativo " + NumToWords( -1 * nNumber )

ElseIf nNumber <> Int( nNumber ) Then
cWords = NumToWords( Int( nNumber ) )
cWords = cWords + " punto"
nFrac = nNumber - Int( nNumber )
Do
nFrac = nFrac * 10
nDigit = Int( nFrac )
nFrac = nFrac - nDigit

If nDigit > 0 Then
cDigitWord = NumToWords( nDigit )
cWords = cWords + " " + cDigitWord
Else
Exit Do
EndIf
Loop

ElseIf nNumber < 20 Then
Select Case nNumber
Case 0: cWords = "cero"
Case 1: cWords = "un"
Case 2: cWords = "dos"
Case 3: cWords = "tres"
Case 4: cWords = "cuatro"
Case 5: cWords = "cinco"
Case 6: cWords = "seis"
Case 7: cWords = "siete"
Case 8: cWords = "ocho"
Case 9: cWords = "nueve"
Case 10: cWords = "diez"
Case 11: cWords = "once"
Case 12: cWords = "doce"
Case 13: cWords = "trece"
Case 14: cWords = "catorce"
Case 15: cWords = "quince"
Case 16: cWords = "dieciseis"
Case 17: cWords = "diecisiete"
Case 18: cWords = "dieciocho"
Case 19: cWords = "diecinueve"
End Select

ElseIf nNumber < 100 Then
nTensPlace = Int( nNumber / 10 )
nOnesPlace = nNumber Mod 10
Select Case nTensPlace * 10
Case 20: cWords = "veinte"
Case 30: cWords = "treinta"
Case 40: cWords = "cuarenta"
Case 50: cWords = "cincuenta"
Case 60: cWords = "sesenta"
Case 70: cWords = "setenta"
Case 80: cWords = "ochenta"
Case 90: cWords = "noventa"
End Select
If nOnesPlace > 0 Then
If nTensPlace = 2 Then
cwords = "veinti" + NumToWords( nOnesPlace)
Else
cWords = cWords + " y " + NumToWords( nOnesPlace )
EndIf
EndIf

ElseIf nNumber < 1000 Then
nHundredsPlace = Int( nNumber / 100 )
nRest = nNumber Mod 100
' cWords = NumToWords( nHundredsPlace )
' cWords = cWords + " ciento"
Select Case nHundredsPlace * 100
Case 100: cWords = "cien"
Case 200: cWords = NumToWords( nHundredsPlace ) + "cientos"
Case 300: cWords = NumToWords( nHundredsPlace ) + "cientos"
Case 400: cWords = NumToWords( nHundredsPlace ) + "cientos"
Case 500: cWords = "quinientos"
Case 600: cWords = NumToWords( nHundredsPlace ) + "cientos"
Case 700: cWords = "setecientos"
Case 800: cWords = NumToWords( nHundredsPlace ) + "cientos"
Case 900: cWords = "novecientos"
End Select

If nRest > 0 Then
If nHundredsPlace = 1 Then
cWords = cWords + "to" + NumToWords( nRest )
Else
cWords = cWords + " " + NumToWords( nRest )
EndIf
EndIf

ElseIf nNumber < 1000000 Then
nThousands = Int( nNumber / 1000 )
nRest = nNumber Mod 1000
If nThousands > 1 Then
cWords = NumToWords( nThousands )
EndIf
cWords = cWords + "mil"
If nRest > 0 Then
cWords = cWords + " " + NumToWords( nRest )
EndIf

ElseIf nNumber < 1000000000 Then
nMillions = Int( nNumber / 1000000 )
nRest = Int( nNumber Mod 1000000 )
cWords = NumToWords( nMillions )
cWords = cWords + " millones"
If nRest > 0 Then
cWords = cWords + " " + NumToWords( nRest )
EndIf

ElseIf nNumber < 1000000000000 Then
nBillions = Int( nNumber / 1000000000 )
nRest = Int( nNumber Mod 1000000000 )
cWords = NumToWords( nBillions )
cWords = cWords + " billones"
If nRest > 0 Then
cWords = cWords + " " + NumToWords( nRest )
EndIf

ElseIf nNumber < 1000000000000000 Then
nTrillions = Int( nNumber / 1000000000000 )
nRest = Int( nNumber Mod 1000000000000 )
cWords = NumToWords( nTrillions )
cWords = cWords + " trillones"
If nRest > 0 Then
cWords = cWords + " " + NumToWords( nRest )
EndIf

' You can follow the pattern of the Millions / Billions / Trillions
' if you need bigger numbers.

EndIf

NumToWords() = cWords
End Function

Function NumToPesos( ByVal nNumber As Double ) As String
cWords = ""

nPesos = Int( nNumber )
nCents = Int( (nNumber - Int( nNumber ) + 0.005) * 100 )

cPesos = NumToWords( nPesos )
cCents = CSTR( nCents )
If Len( cCents ) < 2 Then
cCents = "0" & cCents
EndIf

cWords = cPesos & " pesos " & cCents & "/100 MN"

NumToPesos() = cWords
End Function
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger
lars
Newbie
Newbie


Joined: 24 Jan 2005
Posts: 1

PostPosted: Mon Jan 24, 2005 8:09 am    Post subject: Re: NumToWords convert 423 to Four Hundred Twenty Three Reply with quote

DannyB wrote:
Code:

...
         Case 19: cWords = "Ninteen"
...



Nitpicking: should be "Nineteen".
Back to top
View user's profile Send private message
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Tue Jan 25, 2005 7:54 am    Post subject: Re: NumToWords convert 423 to Four Hundred Twenty Three Reply with quote

lars wrote:
Nitpicking: should be "Nineteen".

Thanks.

Nitpicking is very time consuming. If children returning from summer camp got lice while they were there, the best treatment is to cut hair very short, and then use the medicated shampoo to kill the eggs. It takes many hours to try to pick the nits out, and you are unlikely to get them all. Especially if the child has long hair.
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
coume
General User
General User


Joined: 27 May 2003
Posts: 36

PostPosted: Thu Apr 21, 2005 11:08 pm    Post subject: Reply with quote

Thanks for this one!

I wanted to code one but running out of time Smile
I need to complete it and you know what will be its use?

Create a OOo doc, that generate a random number and ask to write it in English, or Spanish or French... So my little cousin will perhaps find more funny to learn how to write correctly the figures Smile

Ludo
_________________
http://www.mythTVtalk.com - Where the World comes to speak about MythTV!
Hundreds of MythTV users already joined the community! What are you waiting to be the next one?
Back to top
View user's profile Send private message Visit poster's website
coume
General User
General User


Joined: 27 May 2003
Posts: 36

PostPosted: Thu Apr 21, 2005 11:50 pm    Post subject: Reply with quote

btw, did it work for someone with the billions and trillions??

I get an error:
Quote:
Inadmissible value or data type
Overflow


I'm running OOo2 beta on Linux

Ludo
_________________
http://www.mythTVtalk.com - Where the World comes to speak about MythTV!
Hundreds of MythTV users already joined the community! What are you waiting to be the next one?
Back to top
View user's profile Send private message Visit poster's website
bordia
General User
General User


Joined: 24 Jul 2006
Posts: 19
Location: Ratlam MP India

PostPosted: Tue Jul 25, 2006 3:22 pm    Post subject: Conversion for Indian Case Reply with quote

Can you modify your function of converting number to word for case in India like :

number : 32651424.48

Rupees Three crores twenty six lacs fifty one thousand four hunderd twenty four and paisa forty eight only.

If you can provide me it will be great help.

Thanks
Back to top
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets All times are GMT - 8 Hours
Goto page 1, 2, 3  Next
Page 1 of 3

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group