如何将数字金额转换为英文描述的金额?(200分)

A

allow

Unregistered / Unconfirmed
GUEST, unregistred user!
如何将数字金额转换为英文描述的金额?

意思是将12,300 转换为英文描述:twelve thousand three hundred
 
[red]哦[/red]
[blue]關注!研究!研究![/blue]
 
数字/1000000
数字/10000
数字/1000
数字/100
然后判断
 
这个是c++的
自己翻译一下吧!


void SetDollarsCents(HWND hWnd, DWORD dwCents);
CString HundredsTensOnes(DWORD dwHundredsTensOnes);
CString TensOnes(DWORD dwTensOnes);


/////////////////////////////////////////////////////////////////////////////
// Public functions

void AFXAPI DDX_DollarsCents(CDataExchange* pDX, int nIDC, DWORD&amp
dwCents)
{
HWND hWndCtrl = pDX->PrepareEditCtrl(nIDC);
if (pDX->m_bSaveAndValidate)
{
if (!GetDollarsCents(hWndCtrl, dwCents))
{
AfxMessageBox(IDS_INVALID_DOLLAR_CENT);
pDX->Fail();
}
}
else
{
SetDollarsCents(hWndCtrl, dwCents);
}
}


BOOL GetDollarsCents(CWnd* pWnd, DWORD&amp
dwCents)
{
ASSERT(pWnd != NULL);
return GetDollarsCents(pWnd->m_hWnd, dwCents);
}

BOOL GetDollarsCents(HWND hWnd, DWORD&amp
dwCents)
{
TCHAR szWindowText[20];
::GetWindowText(hWnd, szWindowText, 19);
DWORD dwDollars;
int nCents;
TCHAR* psz;
TCHAR* pszDollars;
TCHAR* pszCents;

// strip leading blanks
for (pszDollars = szWindowText
*pszDollars == ' '
pszDollars++)
{
if (*pszDollars == 0)
{
dwCents = 0;
return TRUE;
}
}

// parse dollar amount, before optional decimal point
for (psz = pszDollars
(*psz != '.') &&amp
(*psz != ' ') &&amp
(*psz != 0)
psz++)
{
if ((*psz < '0') || (*psz > '9'))
return FALSE;
}
BOOL bDollarsOnly = (*psz == 0);
*psz = 0;

if (_tcslen(pszDollars) > 8)
return FALSE;
if (_tcslen(pszDollars) == 0)
{
dwDollars = 0L;
}
else
{
dwDollars = _ttol(pszDollars);
if (dwDollars > ((DWORD)0xffffffff)/100)
return FALSE;
}

if (bDollarsOnly)
{
nCents = 0;
}
else // decimal point was found
{
// parse cents
for (pszCents = ++psz
(*psz != 0) &amp;&amp
(*psz != ' ')
psz++)
{
if ((*psz < '0') || (*psz > '9'))
return FALSE;
}
if (*psz == ' ')
{
for (psz++
*psz != 0
psz++)
{
if (*psz != ' ')
return FALSE;
}
}

int nCentsStrLen = _tcslen(pszCents);
switch (nCentsStrLen)
{
case 0:
nCents = 0;
break;
case 1:
nCents = _ttoi(pszCents) * 10;
break;
case 2:
nCents = _ttoi(pszCents);
break;
default:
return FALSE;
}
}

dwCents = dwDollars * 100 + nCents;
return TRUE;
}


void SetDollarsCents(CWnd* pWnd, DWORD dwCents)
{
ASSERT(pWnd != NULL);
SetDollarsCents(pWnd->m_hWnd, dwCents);
}

void SetDollarsCents(HWND hWnd, DWORD dwCents)
{
// Convert the DWORD dollars/cents value to a string and
// display it in the dollars/cents control.

// If the dollar cent field has been previously determined by
// DDX_DollarsCents() to be invalid, then don't update it.
// Leave the invalid data in the field so the user can correct
// it, rather than replace it with the literal translation
// of the INVALID_DOLLARS_CENTS #define value.

if (dwCents == INVALID_DOLLARS_CENTS)
return;

CString str = GetDollarsCentsFormatted(dwCents);
::SetWindowText(hWnd, str.GetBufferSetLength(20));
}


CString GetDollarsCentsFormatted(DWORD dwCents)
{
if (dwCents == INVALID_DOLLARS_CENTS)
{
CString str;
str.LoadString(IDS_UNKNOWN);
return str;
}

DWORD dwDollars = dwCents / 100;
WORD wCents = (WORD)(dwCents - 100 * dwDollars);

CString str;
str.Format(_T("%lu.%02u"), dwDollars, wCents);
return str;
}


CString GetDollarsCentsText(DWORD dwCents)
{
CString str, strTemp;
if (dwCents == INVALID_DOLLARS_CENTS)
{
str.LoadString(IDS_UNKNOWN);
return str;
}

DWORD dwDollars = dwCents / 100;
WORD wCents = (WORD)(dwCents - (dwDollars * 100));
if (dwDollars == 0L)
{
str.LoadString(IDS_ONES_0)
// "Zero"
str += ' ';
}
else
{
if (dwDollars >= 1000000)
{
DWORD dwMillions = dwDollars / 1000000;
CString strMillions = HundredsTensOnes(dwMillions);
strTemp.LoadString(IDS_MILLION)
// "Million"
str = strMillions;
str += ' ';
str += strTemp;
str += ' ';
dwDollars -= (dwMillions * 1000000);
}
if (dwDollars >= 1000)
{
DWORD dwThousands = dwDollars / 1000;
CString strThousands = HundredsTensOnes(dwThousands);
strTemp.LoadString(IDS_THOUSAND)
// "Thousand"
str += strThousands;
str += ' ';
str += strTemp;
str += ' ';
dwDollars -= (dwThousands * 1000);
}
if (dwDollars > 0)
{
CString strHundredsTensOnes = HundredsTensOnes(dwDollars);
str += strHundredsTensOnes;
str += ' ';
}
}
TCHAR szCents[10];
CString strCents(_itot(wCents, szCents, 10));
strTemp.LoadString(IDS_AND)
// "and"
str += strTemp;
str += ' ';
str += strCents;
strTemp.LoadString(IDS_HUNDRETHS_DOLLARS)
// "/100ths Dollars"
str += strTemp;
return str;
}

/////////////////////////////////////////////////////////////////////////////
// Implementation

CString HundredsTensOnes(DWORD dwHundredsTensOnes)
{
CString str, strTemp;
if (dwHundredsTensOnes >= 100)
{
DWORD dwHundreds = dwHundredsTensOnes / 100;
CString strHundreds;
strHundreds.LoadString(IDS_ONES_0 + dwHundreds);
strTemp.LoadString(IDS_HUNDRED);
str = strHundreds;
str += ' ';
str += strTemp;
str += ' ';
dwHundredsTensOnes -= (dwHundreds * 100);
}
if (dwHundredsTensOnes > 0)
{
CString strTensOnes = TensOnes(dwHundredsTensOnes);
str += strTensOnes;
}
return str;
}


CString TensOnes(DWORD dwTensOnes)
{
CString str, strTemp;
if (dwTensOnes > 19)
{
DWORD dwTens = dwTensOnes / 10;
strTemp.LoadString(IDS_TENS_0 + dwTens);
str += strTemp;
dwTensOnes -= (dwTens * 10);
if (dwTensOnes > 0)
{
CString strOnes;
strOnes.LoadString(IDS_ONES_0 + dwTensOnes);
str += '-';
str += strOnes;
}
}
else
if (dwTensOnes >= 10)
{
CString strTeens;
strTeens.LoadString(IDS_TEENS_10 + dwTensOnes - 10);
str += strTeens;
}
else
{
CString strOnes;
strOnes.LoadString(IDS_ONES_0 + dwTensOnes);
str += strOnes;
}
return str;
}
 
to: hpretty
不好意思,我不会用C,要是能给一个delphi的就非常感谢了.

哪位仁兄能做出delphi的一样有分!
 
其实和中文的区别不大,给你份中文的参考

http://www.delphibbs.com/delphibbs/dispq.asp?lid=666998
 
我的问题解决了,下面是我的程序!
有类似问题的朋友可以参考,也谢谢上面几位朋友!
dict_1[1] := 'one';
dict_1[2] := 'two';
dict_1[3] := 'three';
dict_1[4] := 'four';
dict_1[5] := 'five';
dict_1[6] := 'six';
dict_1[7] := 'seven';
dict_1[8] := 'eight';
dict_1[9] := 'nine';
dict_1[10] := 'ten';
dict_1[11] := 'eleven';
dict_1[12] := 'twelve';
dict_1[13] := 'thirteen';
dict_1[14] := 'fourteen';
dict_1[15] := 'fifteen';
dict_1[16] := 'sixteen';
dict_1[17] := 'seventeen';
dict_1[18] := 'eighteen';
dict_1[19] := 'nineteen';
dict_1[20] := 'twenty';
dict_1[30] := 'thirty';
dict_1[40] := 'fourty';
dict_1[50] := 'fifty';
dict_1[60] := 'sixty';
dict_1[70] := 'seventy';
dict_1[80] := 'eighty';
dict_1[90] := 'ninety';

procedure Tfrm_main.Button7Click(Sender: TObject);
var
inparm,outparm,s1,s2,s3,inpoint:string
nlengh,nn,mm,plengh,i:integer;
dl_2,dl_1: real;

//dd,dd1:widestring;
begin
if trim(price1.text)='' then exit;
dl_1:=int(strtofloat(trim(price1.text)));
dl_2:=Frac(strtofloat(trim(price1.text)));
inparm := floattostr(dl_1);
nlengh := length(inparm);
inpoint := floattostr(dl_2);
outparm:=cl_int(inparm);

if inpoint <> '0.00' then
begin
if strtofloat(inparm) <> 0 then
outparm := outparm + ' point'
else
outparm := outparm + 'zero point';
inpoint := copy(inpoint,3,length(inpoint));
for i := 1 to length(inpoint) do
begin
nn := strtoint(copy(inpoint,i,1));
if nn = 0 then
if i<>length(inpoint) then
outparm :=outparm + ' zero'
else
outparm :=outparm
else
outparm :=outparm + ' ' +dict_1[nn];
end;
say_total.text:=outparm;

end;
end;
function Tfrm_main.cl_int(str:string):string;
var
inparm,outparm,s1,s2,s3,inpoint:string
nlengh,nn,mm,plengh,i:integer;
dl_2,dl_1: real;
begin
//转换整数部分
inparm:=str;
nlengh:=length(inparm);
case nlengh of
1:
begin
nn := strtoint(inparm);
if nn <> 0 then
outparm := dict_1[nn]
else
outparm :='';

end;
2:begin
nn := strtoint(inparm);
mm := nn mod 10;

if (mm = 0) or (copy(inparm,1,1) = '1') then
outparm := dict_1[nn]
else
begin
s1 := copy(inparm,1,1);
s2 := copy(inparm,2,1);
s1 := floattostr(strtofloat(s1) * 10 );
outparm := cl_int(s1) + '-' + cl_int(s2);
end;
end
3:begin
s1 := copy(inparm,1,1);
s2 := copy(inparm,2,2);
if s2 <> '00' then
outparm := cl_int(s1) + ' hundred and ' + cl_int(s2)
else
outparm := cl_int(s1) + ' hundred ' + cl_int(s2);
end;
4:begin
s1 := copy(inparm,1,1);
s2 := copy(inparm,2,3);
outparm := cl_int(s1) + ' thousand ' + cl_int(s2);
end;
5:begin
s1 := copy(inparm,1,2);
s2 := copy(inparm,3,3);
outparm := cl_int(s1) + ' thousand ' + cl_int(s2);
end;
6:begin
s1 := copy(inparm,1,3);
s2 := copy(inparm,4,3);
outparm := cl_int(s1) + ' thousand ' + cl_int(s2);
end;
7:begin
s1 := copy(inparm,1,1);
s2 := copy(inparm,2,6);
outparm := cl_int(s1) + ' million ' + cl_int(s2);
end;
8:begin
s1 := copy(inparm,1,2);
s2 := copy(inparm,3,6);
outparm := cl_int(s1) + ' million ' + cl_int(s2);
end;
9:begin
s1 := copy(inparm,1,3);
s2 := copy(inparm,4,6);
outparm := cl_int(s1) + ' million ' + cl_int(s2);
end;
end;
cl_int:=outparm;
end;
 
{**************************************************}
{ }
{ Number to letters unit version 1.0 }
{ }
{ copyright (C) Dylan Thomas 2000 }
{ }
{ License: No significant restrictions. }
{ }
{ Language: US. English }
{ }
{**************************************************}

unit NumberToLetters;

interface

{var

Calls: Integer;} //Use to keep track of the number of recursive calls

(* This function returns the written equivalent of a number. *)
function NumToLetters(Number: Real): string;

implementation
uses SysUtils;

type

TNumberStr = string[13];

const
Numbers: array[1..19] of TNumberStr = ('one', 'two', 'three', 'four',
'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve',
'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen',
'nineteen');

Tenths: array[1..9] of TNumberStr = ('ten', 'twenty', 'thirty', 'forty',
'fifty', 'sixty', 'seventy', 'eighty', 'ninety');

ErrorString = 'not in valid range';

Min = 1.00;
Max = 4294967295.99;

function NumToLetters(Number: Real): string;

function RecurseNumber(N: LongWord): string;
begin
{Inc(Calls);} //Use to keep track of the number of recursive calls
case N of
1..19:
Result := Numbers[N];
20..99:
Result := Tenths[N div 10] + ' ' + RecurseNumber(N mod 10);
100..999:
Result := Numbers[N div 100] + ' hundred ' + RecurseNumber(N mod 100);
1000..999999:
Result := RecurseNumber(N div 1000) + ' thousand ' +
RecurseNumber(N mod 1000);
1000000..999999999: Result := RecurseNumber(N div 1000000) + ' million '
+ RecurseNumber(N mod 1000000);
1000000000..4294967295: Result := RecurseNumber(N div 1000000000) +
' billion ' + RecurseNumber(N mod 1000000000);
end
{Case N of}
end
{RecurseNumber}

begin
{Calls := 0;} //Use to keep track of the number of recursive calls
if (Number >= Min) and (Number <= Max) then
begin
Result := RecurseNumber(Round(Int(Number)));
{Added for cents in a currency value}
if not(Frac(Number) = 0.00) then
Result := Result + ' and ' + IntToStr(Round(Frac(Number) * 100)) +
'/100';
end
else
raise ERangeError.CreateFmt('%g ' + ErrorString + ' %g..%g',
[Number, Min, Max]);
end;{NumToLetters}

end.
 
多人接受答案了。
 
顶部