Данный код "считает" до миллиона долларов. Поэкспериментируйте
с ним - попробуйте "посчитать" до миллиарда, конвертировать ее в рубли или
переделать ее для работы с русским языком. Только не забудьте прислать мне ваши
решения!
unit uNum2Str;
// Possible enhancements// Move strings out to resource files// Put in a general num2str utilityinterfacefunction Num2Dollars(dNum: double): string;
implementationuses SysUtils;
function LessThan99(dNum: double): string; forward;
// floating point modulusfunction FloatMod(i, j: double): double;
begin
result := i - (Int(i / j) * j);
end;
function Hundreds(dNum: double): string;
var
workVar: double;
beginif (dNum < 100) or (dNum > 999) thenraise Exception.Create('hundreds range exceeded');
result := '';
workVar := Int(dNum / 100);
if workVar > 0 then
result := LessThan99(workVar) + ' Hundred';
end;
function OneToNine(dNum: Double): string;
beginif (dNum < 1) or (dNum > 9) thenraise exception.create('onetonine: value out of range');
result := 'woops';
if dNum = 1 then
result := 'One'
elseif dNum = 2 then
result := 'Two'
elseif dNum = 3 then
result := 'Three'
elseif dNum = 4 then
result := 'Four'
elseif dNum = 5.0 then
result := 'Five'
elseif dNum = 6 then
result := 'Six'
elseif dNum = 7 then
result := 'Seven'
elseif dNum = 8 then
result := 'Eight'
elseif dNum = 9 then
result := 'Nine';
end;
function ZeroTo19(dNum: double): string;
beginif (dNum < 0) or (dNum > 19) thenraise Exception.Create('Bad value in dNum');
result := '';
if dNum = 0 then
result := 'Zero'
elseif (dNum <= 1) and (dNum >= 9) then
result := OneToNine(dNum)
elseif dNum = 10 then
result := 'Ten'
elseif dNum = 11 then
result := 'Eleven'
elseif dNum = 12 then
result := 'Twelve'
elseif dNum = 13 then
result := 'Thirteen'
elseif dNum = 14 then
result := 'Fourteen'
elseif dNum = 15 then
result := 'Fifteen'
elseif dNum = 16 then
result := 'Sixteen'
elseif dNum = 17 then
result := 'Seventeen'
elseif dNum = 18 then
result := 'Eighteen'
elseif dNum = 19 then
result := 'Nineteen'
else
result := 'woops!';
end;
function TwentyTo99(dNum: double): string;
var
BigNum: string;
beginif (dNum < 20) or (dNum > 99) thenraise exception.Create('TwentyTo99: dNum out of range!');
BigNum := 'woops';
if dNum >= 90 then
BigNum := 'Ninety'
elseif dNum >= 80 then
BigNum := 'Eighty'
elseif dNum >= 70 then
BigNum := 'Seventy'
elseif dNum >= 60 then
BigNum := 'Sixty'
elseif dNum >= 50 then
BigNum := 'Fifty'
elseif dNum >= 40 then
BigNum := 'Forty'
elseif dNum >= 30 then
BigNum := 'Thirty'
elseif dNum >= 20 then
BigNum := 'Twenty';
// lose the big num
dNum := FloatMod(dNum, 10);
if dNum > 0.00 then
result := BigNum + ' ' + OneToNine(dNum)
else
result := BigNum;
end;
function LessThan99(dNum: double): string;
beginif dNum <= 19 then
result := ZeroTo19(dNum)
else
result := TwentyTo99(dNum);
end;
function Num2Dollars(dNum: double): string;
var
centsString: string;
cents: double;
workVar: double;
begin
result := '';
if dNum < 0 thenraise Exception.Create('Negative numbers not supported');
if dNum > 999999999.99 thenraise
Exception.Create('Num2Dollars only supports up to the millions at this point!');
cents := (dNum - Int(dNum)) * 100.0;
if cents = 0.0 then
centsString := 'and 00/100 Dollars'
elseif cents < 10 then
centsString := Format('and 0%1.0f/100 Dollars', [cents])
else
centsString := Format('and %2.0f/100 Dollars', [cents]);
dNum := Int(dNum - (cents / 100.0)); // lose the cents// deal with million'sif (dNum >= 1000000) and (dNum <= 999999999) thenbegin
workVar := dNum / 1000000;
workVar := Int(workVar);
if (workVar <= 9) then
result := ZeroTo19(workVar)
elseif (workVar <= 99) then
result := LessThan99(workVar)
elseif (workVar <= 999) then
result := Hundreds(workVar)
else
result := 'mill fubar';
result := result + ' Million';
dNum := dNum - (workVar * 1000000);
end;
// deal with 1000'sif (dNum >= 1000) and (dNum <= 999999.99) thenbegin// doing the two below statements in one line of code yields some really// freaky floating point errors
workVar := dNum / 1000;
workVar := Int(workVar);
if (workVar <= 9) then
result := ZeroTo19(workVar)
elseif (workVar <= 99) then
result := LessThan99(workVar)
elseif (workVar <= 999) then
result := Hundreds(workVar)
else
result := 'thou fubar';
result := result + ' Thousand';
dNum := dNum - (workVar * 1000);
end;
// deal with 100'sif (dNum >= 100.00) and (dNum <= 999.99) thenbegin
result := result + ' ' + Hundreds(dNum);
dNum := FloatMod(dNum, 100);
end;
// format in anything less than 100if (dNum > 0) or ((dNum = 0) and (Length(result) = 0)) thenbegin
result := result + ' ' + LessThan99(dNum);
end;
result := result + ' ' + centsString;
end;
end.
Статья Число прописью 5 раздела Синтаксис Текст и Строки может быть полезна для разработчиков на Delphi и FreePascal.
Комментарии и вопросы
Материалы статей собраны из открытых источников, владелец сайта не претендует на авторство. Там где авторство установить не удалось, материал подаётся без имени автора. В случае если Вы считаете, что Ваши права нарушены, пожалуйста, свяжитесь с владельцем сайта.