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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need a numeric verbalizer: 1210 to One thousand two hundred and ten. 1

Status
Not open for further replies.

dsandlin

Programmer
Jul 11, 2003
56
0
0
CR
I'm making a check writer report and I need a function to convert numbers into verbal descriptions (e.g., 1210.25 into "One thousand two hundred ten and 25/100". Before I go to the trouble of writing one, I thought perhaps that someone had already done it. Does anyone have one?

Thanks, Dick Sandlin
 
Thanks a lot! That is exactly what I needed.


Regards, Dick
 

Here is another approach. I copied it once from another site. As far as I remeber, the author (didn't write down his name, sorry) allowed to modify the code to your heart's content. It could have been slightly modified since then, but 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