二级VFP如何将数字转换成英文的说法

出处:www.examlink.com 作者:小罗 日期:2007年01月24日 13时55分

  HONGKAI:

  VFP6

  WINDOWS98

  如何将数字(如“123”)转换成英文的说法?onehundandandtwenty-three用程序转换。

  回答:

  这里有这个VB程序是做这件事,你可以略加修改以适应VFP(如果你将其转换为VFP程序,希望你能寄一份给我)。

  PrivateFunctionNumToText(dblValAsDouble)AsString

  StaticOnes(0To9)AsString

  StaticTeens(0To9)AsString

  StaticTens(0To9)AsString

  StaticThousands(0To4)AsString

  StaticbInitAsBoolean

  DimiAsInteger,bAllZerosAsBoolean,bShowThousandsAsBoolean

  DimstrValAsString,strBuffAsString,strTempAsString

  DimnColAsInteger,nCharAsInteger

  'Onlyhandlespositivevalues

  Debug。AssertdblVal>=0

  IfbInit=FalseThen

  'Initializearray

  bInit=True

  Ones(0)="zero"

  Ones(1)="one"

  Ones(2)="two"

  Ones(3)="three"

  Ones(4)="four"

  Ones(5)="five"

  Ones(6)="six"

  Ones(7)="seven"

  Ones(8)="eight"

  Ones(9)="nine"

  Teens(0)="ten"

  Teens(1)="eleven"

  Teens(2)="twelve"

  Teens(3)="thirteen"

  Teens(4)="fourteen"

  Teens(5)="fifteen"

  Teens(6)="sixteen"

  Teens(7)="seventeen"

  Teens(8)="eighteen"

  Teens(9)="nineteen"

  Tens(0)=""

  Tens(1)="ten"

  Tens(2)="twenty"

  Tens(3)="thirty"

  Tens(4)="forty"

  Tens(5)="fifty"

  Tens(6)="sixty"

  Tens(7)="seventy"

  Tens(8)="eighty"

  Tens(9)="ninety"

  Thousands(0)=""

  Thousands(1)="thousand"'USnumbering

  Thousands(2)="million"

  Thousands(3)="billion"

  Thousands(4)="trillion"

  EndIf

  'Traperrors

  OnErrorGoToNumToTextError

  'Getfractionalpart

  strBuff="and"&Format((dblVal-Int(dblVal))*100,"00")&"/100"

  'Convertresttostringandprocesseachdigit

  strVal=CStr(Int(dblVal))

  'Non-zerodigitnotyetencountered

  bAllZeros=True

  'Iteratethroughstring

  Fori=Len(strVal)To1Step-1

  'Getvalueofthisdigit

  nChar=Val(Mid$(strVal,i,1))

  'Getcolumnposition

  nCol=(Len(strVal)-i)+1

  'Actiondependson1's,10'sor100'scolumn

  SelectCase(nColMod3)

  Case1'1'sposition

  bShowThousands=True

  Ifi=1Then

  'Firstdigitinnumber(lastinloop)

  strTemp=Ones(nChar)&""

  ElseIfMid$(strVal,i-1,1)="1"Then

  'Thisdigitispartof"teen"number

  strTemp=Teens(nChar)&""

  i=i-1'Skiptensposition

  ElseIfnChar>0Then

  'Anynon-zerodigit

  strTemp=Ones(nChar)&""

  Else

  'Thisdigitiszero。Ifdigitintensandhundredscolumn

  'arealsozero,don'tshow"thousands"

  bShowThousands=False

  'Testfornon-zerodigitinthisgrouping

  IfMid$(strVal,i-1,1)<>"0"Then

  bShowThousands=True

  ElseIfi>2Then

  IfMid$(strVal,i-2,1)<>"0"Then

  bShowThousands=True

  EndIf

  EndIf

  strTemp=""

  EndIf

  'Show"thousands"ifnon-zeroingrouping

  IfbShowThousandsThen

  IfnCol>1Then

  strTemp=strTemp&Thousands(nCol\3)

  IfbAllZerosThen

  strTemp=strTemp&""

  Else

  strTemp=strTemp&","

  EndIf

  EndIf

  'Indicatenon-zerodigitencountered

  bAllZeros=False

  EndIf

  strBuff=strTemp&strBuff

  Case2'10'sposition

  IfnChar>0Then

  IfMid$(strVal,i+1,1)<>"0"Then

  strBuff=Tens(nChar)&"-"&strBuff

  Else

  strBuff=Tens(nChar)&""&strBuff

  EndIf

  EndIf

  Case0'100'sposition

  IfnChar>0Then

  strBuff=Ones(nChar)&"hundred"&strBuff

  EndIf

  EndSelect

  Nexti

  'Convertfirstlettertouppercase

  strBuff=UCase$(Left$(strBuff,1))&Mid$(strBuff,2)

  EndNumToText:

  'Returnresult

  NumToText=strBuff

  ExitFunction

  NumToTextError:

  strBuff="#Error#"

  ResumeEndNumToText

  EndFunction

最后更新时间:2008-05-28 11:40:50
文章评论
共有 0 位网友发表了评论
用户名: 新注册) 密码: 匿名评论 [查看所有评论]

评论内容:(不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
您可以用以下几种方式找到此文章

考试全流程