Code archives/Algorithms/Convert Integers to Words

This code has been declared by its author to be Public Domain code.

Download source code

Convert Integers to Words by _PJ_2005
Newly Updated (revision: 1st October, 2009)

-Changes:

Large Optimisation, Increased range capacity, Cleaned code and Efficiency.

These functions take a passed Integer, i.e. 305632 and convert the number to descriptive text such as "THREE HUNDRED AND FIVE THOUSAND, SIX HUNDRED AND THIRTY-TWO"

To call the function use:

IntToString$(Integer%)

The Function can now handle both negative and 0 integers, however, for values with magnitude over 999,999,999 a generic result is given.
;Convert Integer To String
;Submitted by Malice

;Updated October 2009

Function IntToString$(nInt)
	If (Not(nInt)) Then Return "None"
	
	Local sReturn$
	If (nInt<0)
		nInt=Abs(nInt)
		sReturn$="Minus "			
		If nInt>(999999999)	Then 	Return "Too Many Less Than Zero"
	End If
	
	If nInt>(999999999)	Then 	Return "Too Many"
			
	Local HTU	=	(nInt Mod 1000)
	Local Thou	=	((nInt Mod 1000000)			-	HTU)	*	0.001
	Local Mill	=	((nInt Mod 1000000000)		-	((Thou	* 1000 )+	HTU))	*	0.000001

	If (Mill) 
		sReturn$	=	sReturn+HundredTen$(Mill)+" Million"
	End If
	
	If (Thou)
		If (Mill)						Then	sReturn$	=	sReturn+", "
		sReturn$	=	sReturn+HundredTen$(Thou,(Mill))+" Thousand"
	End If
	
	If (HTU)
		If ((Mill)	Or	(Thou))	Then	sReturn$	=	sReturn+", "
		sReturn$=sReturn+HundredTen$(HTU,(Mill+Thou))
	End If

	Return CleanString$(sReturn$)
End Function
			
Function HundredTen$(nInt,nBefore=False)
	
	Local sReturn$
	
	Local	Tens		=	(Int(Floor(Float(nInt * 0.1)) Mod 10))
	Local	Units		=	(Int(nInt Mod 10))
	If (Tens<2)
		Units			=	(nInt Mod 100)
		Tens			=	0
	End If
		
	Local	Hund	=	((nInt-((Tens*10)+Units))	*0.01)
		
	If (Hund)
		sReturn$		=	sReturn$+Unit$(Hund)+"-Hundred"
	End If
		
	If (Not(Tens+Units))
		Return sReturn$
	Else
		If ((Hund)+(nBefore))
			sReturn$=sReturn$+" And "
		End If
	End If					
	
	If (Tens)	
		Select (Tens)
			Case 2	:	sReturn$=sReturn$+"Twenty"
						If (Units) Then sReturn=sReturn$+"-"
			Case 3	:	sReturn$=sReturn$+"Thirty"
						If (Units) Then sReturn=sReturn$+"-"
			Case 4	:	sReturn$=sReturn$+"Forty"
						If (Units) Then sReturn=sReturn$+"-"
			Case 5	:	sReturn$=sReturn$+"Fifty"
						If (Units) Then sReturn=sReturn$+"-"
			Default	:	sReturn$=Replace(sReturn$+Unit(Tens)+"ty","tty","ty")
						If (Units) Then sReturn=sReturn$+"-"
		End Select
	End If
	
	If (Units) Then sReturn$=sReturn$+Unit(Units)
	
	Return sReturn$
	
End Function	

Function Unit$(nInt)
	Select nInt
		Case 0	:	Return ""
		Case 1	:	Return "One"
		Case 2	:	Return "Two"
		Case 3	:	Return "Three"
		Case 4	:	Return "Four"
		Case 5	:	Return "Five"
		Case 6	:	Return "Six"
		Case 7	:	Return "Seven"
		Case 8	:	Return "Eight"
		Case 9	:	Return "Nine"
		Case 10	:	Return "Ten"
		Case 11	:	Return "Eleven"
		Case 12	:	Return "Twelve"
		Case 13	:	Return "Thirteen"
		Case 15	:	Return "Fifteen"
		Default	:	Return Replace(Unit$(nInt Mod 10)+"teen","tteen","teen")
	End Select
End Function

Function CleanString$(sReturn$)
	sReturn$=Trim$(sReturn$)
	sReturn$=Replace (sReturn$,"  "," ")
	sReturn$=Replace (sReturn$,", And"," And")
	If(Left$(sReturn$,3)="And")	Then	sReturn$=Right$(sReturn$,(Len(sReturn$)-4))
	If(Right$(sReturn$,3)="And")	Then	sReturn$=Left$(	sReturn$,(Len(sReturn$)-4))

	Return sReturn$+"."
End Function

Comments

_PJ_2009
Updated.


TaskMaster2009
Hmm, just looked at this. It is actually proper to have the text say:

THREE HUNDRED FIVE THOUSAND, SIX HUNDRED THIRTY-TWO

Not

THREE HUNDRED AND FIVE THOUSAND, SIX HUNDRED AND THIRTY-TWO

The ands shouldn't be there. Technicality in writing out numbers, "and" should only be used in place of the decimal where the whole number is separated from the fraction portion.


_PJ_2009
The ands shouldn't be there. Technicality in writing out numbers, "and" should only be used in place of the decimal where the whole number is separated from the fraction portion

1) I think the word you are looking for is 'technically'.
2) There is no generalised 'should' and shouldn't or 'proper'.

Conventions which dictate grammatical structure do vary between languages.

I didn't include any MUI or regional. I'm English and by default, I code for "United Kingdom English" use.

If you wish to take the "And"s out for American use, it may be a case of removing this snippet from HundredTen$()
Else
		If ((Hund)+(nBefore))
			sReturn$=sReturn$+" And "
		End If



Brucey2009
It looks correct with "And" in there.
But then I'm from Scotland, so what would I know? :-p


Ked2009
I always thought both would work.

Which one is more correct?: A hundred, or one hundred.


plash2009
Which one is more correct?: A hundred, or one hundred.
One hundred.


_PJ_2009
I'm pretty sure that 'a' or 'one' are mutually interchangeable in most circumstances, however to differentiate between 'A' and 'One' in the code may be problematic, inefficient (as in more inefficient than all the string manipulation there already!), and unnecessary.
Particularly in cases where a string may end up with "TWENTY-A" instead of "TWENTY-ONE" for example.


TaskMaster2009
Hmmm...

http://www.ehow.com/how_4715601_write-numbers-words.html

Is there really a different system in England and other english speaking countries?


_PJ_2009
It's hardly a 'different system', simply as I stated earlier, the conventions for grammatical structure do vary between languages.

American English is a separate language from the English used in the UK. Similarly, Australian English is another different language.

I don't see any relevance or reasonable potential in debating the differences between languages, only a scant few of the code posts in these archives caters for other languages (I note some that have German and English) and it's unreasonable to expect anyone to do so.

As it happens, I explained my reasons for conforming to my native language as well as noting a feature change to accommodate American English.

Not cited as any basis for fact, it's wikipedia after all, but it may be interesting:
http://en.wikipedia.org/wiki/American_and_British_English_differences


TaskMaster2009
I am genuinely just trying to find out, not trying to debate or argue with you.

On the internet, you get the opportunity to interact with people from all over the world. Not having been to England, I find it interesting, the differences between our languages. The only other way you see the differences is in reading, which also brings up differences. Like how you guys spell words with a U in them armour, colour, etc...

I just want to be clear, that I was not trying to debate or argue with you, I am truly interested.

I could not find anything on google dealing with numbers and the use of the word "and" for British English, and I did search out of curiosity.


_PJ_2009
Alright, it's just that your initial post seemed rather accusatory, using words like "should" and "proper". So I'm sorry if I came across as defensive.

I actually tried to search a little myself, but the majority of web-based content on English grammar appears to be american-sourced.

there are a lot of differences between the two 'versions' of English, the internet and global communitcations as well as popular culture influences are gradually eroding some of these differences aweay (an eviolution of global communication maybe, but that's another topic entirely!)

The majority of differences are in spelling, such as dropping the 'u' from Colour or Labour, swapping 's' for 'z' (zed not zee :P ) in words such as Analyse, grammatical differences are more subtle and varied across many different aspects of the linguistics.


TaskMaster2009
Thanks. I didn't realize my first post would be taken as accusatory, I did not mean it that way.

I just find these small differences in languages and customs very interesting. Like a scene in Inglorious Bastards, when one of the good guys (American or British, I don't remember) was impersonating a Nazi, and he orders 3 beers by holding up the first three fingers on his hand and the Nazi Officer knows he isn't German, because Germans hold up their thumb and first two fingers when for displaying three on a hand.

I find subtle differences like this very interesting...


ShadowTurtle2009
The varity is not important. It is the understanding of the language. We do life in 2009 and soon in 2010... it is ok

by the way ... ... ... .. .. . varity is a available part in all languages.


Code Archives Forum