📄 numbertowords.pas
字号:
unit NumberToWords;
interface
uses
Dialogs,
SysUtils, Classes;
const
Ones: array[0..9] of string = ('Nol ', 'Satu ', 'Dua ', 'Tiga ', 'Empat ', 'Lima ', 'Enam ', 'Tujuh ', 'Delapan ', 'Sembilan ');
Teens: array[10..19] of string = ('Sepuluh ', 'Sebelas ', 'Dua Belas ', 'Tiga Belas ', 'Empat Belas ', 'Lima Belas ', 'Enam Belas ', 'Tujuh Belas ', 'Delapan Belas ', 'Sembilan Belas ');
Tens: array[2..9] of string = ('Dua Puluh ', 'Tiga Puluh ', 'Empat Puluh ', 'Lima Puluh ', 'Enam Puluh ', 'Tujuh Puluh ', 'Delapan Puluh ', 'Sembilan Puluh ');
Suffix: array[0..5] of string = ('Ratus ', 'Ribu ', 'Juta ', 'Milyar ', 'Triliun ', 'Biliun ');
var
fs : TFormatSettings;
function ConvertToWords(const Number: double; UseCurrency: Boolean = True):string;
implementation
function ConvertToWords(const Number: double; UseCurrency: Boolean): string;
function DoHundreds(const NumStr: string): string;
var
iCount: integer;
iCurNum: integer;
bFinished: Boolean;
sLocNum: string;
begin
sLocNum := NumStr;
bFinished := False;
case Length(sLocNum) of
1: sLocNum := '00' + sLocNum;
2: sLocNum := '0' + sLocNum;
end;
for iCount := 1 to 3 do
begin
iCurNum := StrToInt(sLocNum[iCount]);
if iCurNum > 0 then
case iCount of
1:
begin
Result := Result + Ones[iCurNum] + Suffix[0];
if Ones[iCurNum] ='Satu ' then
Result := 'Se' + Suffix[0];
if strtoint(copy(sLocNum, 2, 2)) > 0 then
Result := Result + '';
end;
2:
begin
if iCurNum = 1 then
begin
iCurNum := strtoint(copy(sLocNum, 2, 2));
Result := Result + Teens[iCurNum];
bFinished := True;
end
else
Result := Result + Tens[iCurNum];
end;
3: if (not bFinished) then
Result := Result + Ones[iCurNum];
end;
end;
end;
var
sIntValue: string;
sDecValue: string;
sSectionVal: string;
slValSections: TStringList;
iCount: integer;
bIsNegative: boolean;
begin
bIsNegative := False;
if Number < 0 then bIsNegative := True;
try
sIntValue := FormatFloat('#,###', trunc(abs(Number)));
sDecValue := Copy(FormatFloat('.#########', frac(abs(Number))), 2,2);
if (Pos('E', sIntValue) > 0) then
begin
Result := 'ERROR:';
exit;
end;
except
Result := 'ERROR:';
exit;
end;
if (Length(sIntValue) <= 3) and (Length(sIntValue) > 0) then Result := DoHundreds(sIntValue)
else if Length(sIntValue) = 0 then Result := ''
else
begin
slValSections := TStringList.Create;
slValSections.Delimiter := fs.ThousandSeparator;
slValSections.DelimitedText := sIntValue;
for iCount := 1 to slValSections.Count - 1 do slValSections.Move(iCount, 0);
for iCount := 0 to slValSections.Count - 1 do
begin
sSectionVal := '';
sSectionVal := DoHundreds(slValSections[iCount]);
if (sSectionVal='Satu ') and (iCount < 2) then sSectionVal:='Se';
if (iCount = 0) then
begin
Result := sSectionVal;
end;
if (iCount > 0) then
begin
if sSectionVal<>'' then
begin
if sSectionVal='Se' then
Result := sSectionVal + LowerCase(Suffix[iCount]) + Result else
Result := sSectionVal + Suffix[iCount] + Result;
end else
begin
Result := sSectionVal + Result;
end;
end;
end;
FreeAndNil(slValSections);
end;
if LastDelimiter(fs.DecimalSeparator, Result) = (length(Result) - 1) then Delete(Result, Length(Result) - 1, 1);
if (Length(sDecValue) > 0) and UseCurrency then
begin
if Result > '' then Result := Result + 'Rupiah ';
if (Length(sDecValue) = 1) then sDecValue := sDecValue + '0';
Result := Result + DoHundreds(copy(sDecValue, 1, 2)) + 'Sen.';
end else if UseCurrency then
begin
if Result > '' then Result := Result + 'Rupiah.'
end else if (Length(sDecValue) > 0) then
begin
Result := Result + 'Desimal.';
for iCount := 1 to Length(sDecValue) do
Result := Result + Ones[strtoint(sDecValue[iCount])];
end;
if UseCurrency and bIsNegative then Result := 'min ' + Result
else if bIsNegative then Result := 'negative ' + Result;
end;
initialization
GetLocaleFormatSettings(0, fs);
finalization
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -