Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Anyone know where I can get a number to word function 1

Status
Not open for further replies.

Lochness30

Programmer
Jun 9, 2004
23
CA
I could write it myself but why reinvent the wheel if it's already out there.

Does anyone know where I could find a function that could convert numbers to words (for checks, etc).

ie
=convert(1212) would return "one thousand two hundred twelve
 
Lochness30,

I found another one. I copied it once from another site, I think it was Universal thread, but I didn't save this info, so unfortunately I cannot properly credit the author (but you can try to find the original on ). As far as I remember, the author allowed to tweak it to your heart's content. It might have been slightly modified since then, but I'm not sure about that either. It seems to work.

Code:
?Num2Words(987654321789.456)
*?Num2Words(-1.00)
*?Num2Words(0.00)

Function Num2Words(lnNumeral, lcCurrency)
Local lnInteger, lnDecimal, lcNum2Str, lcString1, lcString2
Local lcString3, lcReturnStr, lnX, lnWidthStr, lcSegment, lnIncrement
Local lcCluster, lnOrigLen
If Type('lnNumeral') # "N"
	Return ""
Endif
lcCurrency = IIf(Type('lcCurrency') # "C", "Dollar(s)", lcCurrency)
lnInteger  = Int(lnNumeral)
lnDecimal  = lnNumeral - lnInteger
*!*	If lnInteger = 0 And lnDecimal = 0
*!*	   Return "Zero"
*!*	Endif

lcNum2Str=Alltrim(Str(lnInteger, 237))				&& AllTrim(BetterStr(lnInteger))
Store Len(lcNum2Str) To lnWidthStr, lnOrigLen

If !Between(lnInteger, -999999999999, 999999999999)	&& -999999999999.99, 999999999999.99)
	Return Replicate('*', lnWidthStr)				&&Len(lcNum2Str))
Endif
lcString1 = "     One  Two  ThreeFour Five Six  SevenEightNine "
*___________"----5---10---15---20---25---30---35---40---45---50"
lcString2 = "Ten      Eleven   Twelve   Thirteen Fourteen Fifteen  Sixteen  SeventeenEighteen Nineteen "
*___________"--------9-------18-------27-------36-------45-------54-------63-------72-------81-------90"
lcString3 = "       Twenty Thirty Forty  Fifty  Sixty  SeventyEighty Ninety "
*___________"------7-----14-----21-----28-----35-----42-----49-----56-----63"

*Store Len(lcNum2Str) To lnWidthStr, lnOrigLen
lcReturnStr = IIf(lnInteger < 0, "Negative ", "")
Do While lnWidthStr > 0 And lnInteger # 0
	lnX = Mod(lnWidthStr, 3)
	lnX = IIf(lnX = 0, 3, lnX)
	lcCluster = AllTrim(Left(lcNum2Str, lnX))
	For lnIncrement = 1 To lnX
		lcSegment = AllTrim(SubStr(lcCluster, lnIncrement, 3))
		If Len(lcSegment) # 2
      		lcReturnStr = AllTrim(lcReturnStr) + " " + ;
						  AllTrim(SubStr(lcString1, Val(Left(lcSegment,1)) * 5 + 1,5))
			If Len(lcSegment) = 3 And Val(Left(lcSegment, 1)) > 0
				lcReturnStr = AllTrim(lcReturnStr) + " Hundred"
			Endif
		Else
			If Val(Left(lcSegment,1)) < 2
				If Val(Left(lcSegment,2)) > 9
					lcReturnStr = AllTrim(lcReturnStr) + " " + ;
								  AllTrim(SubStr(lcString2, Val(Right(lcSegment,1)) * 9 + 1,9))
					Exit
				Endif
			Else
				lcReturnStr = AllTrim(lcReturnStr) + " " + ;
							  AllTrim(SubStr(lcString3, (Val(Left(lcSegment,1))-1) * 7 + 1,7))
				If Right(lcSegment,1) = "0"
					Exit
				Endif
			Endif
		Endif
	Next

	Do Case
		Case Len(lcNum2Str) > 9 And Val(lcCluster) # 0
			lcReturnStr = AllTrim(lcReturnStr) + " Billion"
		Case Len(lcNum2Str) > 6 And Val(lcCluster) # 0
			lcReturnStr = AllTrim(lcReturnStr) + " Million"
		Case Len(lcNum2Str) > 3 And Val(lcCluster) # 0
			lcReturnStr = AllTrim(lcReturnStr) + " Thousand"
	Endcase

	lnWidthStr = lnWidthStr - lnX
	lcNum2Str = Right(lcNum2Str, lnWidthStr)
Enddo

lcReturnStr = AllTrim(lcReturnStr) + " " + IIf(Empty(lcReturnStr), "", lcCurrency)
*!*	If lnDecimal # 0.00
lcReturnStr = lcReturnStr + IIf(lnInteger = 0 And Empty(AllTrim(lcReturnStr)), "Negative ", " And ")
lcReturnStr = lcReturnStr + AllTrim(Str(Abs(lnDecimal * 100), 2, 2)) + "/100 Cents"
*!*	Endif

Return AllTrim(lcReturnStr)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top