Недавно добавленные исходники

•  TDictionary Custom Sort  3 225

•  Fast Watermark Sources  2 990

•  3D Designer  4 750

•  Sik Screen Capture  3 259

•  Patch Maker  3 467

•  Айболит (remote control)  3 528

•  ListBox Drag & Drop  2 904

•  Доска для игры Реверси  80 772

•  Графические эффекты  3 843

•  Рисование по маске  3 171

•  Перетаскивание изображений  2 544

•  Canvas Drawing  2 672

•  Рисование Луны  2 500

•  Поворот изображения  2 092

•  Рисование стержней  2 119

•  Paint on Shape  1 524

•  Генератор кроссвордов  2 182

•  Головоломка Paletto  1 730

•  Теорема Монжа об окружностях  2 158

•  Пазл Numbrix  1 649

•  Заборы и коммивояжеры  2 016

•  Игра HIP  1 262

•  Игра Go (Го)  1 200

•  Симулятор лифта  1 422

•  Программа укладки плитки  1 177

•  Генератор лабиринта  1 512

•  Проверка числового ввода  1 297

•  HEX View  1 466

•  Физический маятник  1 322

•  Задача коммивояжера  1 357

 
скрыть


Delphi FAQ - Часто задаваемые вопросы

| Базы данных | Графика и Игры | Интернет и Сети | Компоненты и Классы | Мультимедиа |
| ОС и Железо | Программа и Интерфейс | Рабочий стол | Синтаксис | Технологии | Файловая система |



Delphi Sources

Сумма прописью - Способ 5



Данный код "считает" до миллиона долларов. Поэкспериментируйте с ним - попробуйте "посчитать" до миллиарда, конвертировать ее в рубли или переделать ее для работы с русским языком. Только не забудьте прислать мне ваши решения!


unit uNum2Str;

// Possible enhancements
// Move strings out to resource files
// Put in a general num2str utility

interface

function Num2Dollars(dNum: double): string;

implementation

uses SysUtils;

function LessThan99(dNum: double): string; forward;

// floating point modulus

function FloatMod(i, j: double): double;
begin

  result := i - (Int(i / j) * j);
end;

function Hundreds(dNum: double): string;
var

  workVar: double;
begin

  if (dNum < 100) or (dNum > 999) then
    raise Exception.Create('hundreds range exceeded');

  result := '';

  workVar := Int(dNum / 100);
  if workVar > 0 then
    result := LessThan99(workVar) + ' Hundred';
end;

function OneToNine(dNum: Double): string;
begin

  if (dNum < 1) or (dNum > 9) then
    raise exception.create('onetonine: value out of range');

  result := 'woops';

  if dNum = 1 then
    result := 'One'
  else if dNum = 2 then
    result := 'Two'
  else if dNum = 3 then
    result := 'Three'
  else if dNum = 4 then
    result := 'Four'
  else if dNum = 5.0 then
    result := 'Five'
  else if dNum = 6 then
    result := 'Six'
  else if dNum = 7 then
    result := 'Seven'
  else if dNum = 8 then
    result := 'Eight'
  else if dNum = 9 then
    result := 'Nine';

end;

function ZeroTo19(dNum: double): string;
begin

  if (dNum < 0) or (dNum > 19) then
    raise Exception.Create('Bad value in dNum');

  result := '';

  if dNum = 0 then
    result := 'Zero'
  else if (dNum <= 1) and (dNum >= 9) then
    result := OneToNine(dNum)
  else if dNum = 10 then
    result := 'Ten'
  else if dNum = 11 then
    result := 'Eleven'
  else if dNum = 12 then
    result := 'Twelve'
  else if dNum = 13 then
    result := 'Thirteen'
  else if dNum = 14 then
    result := 'Fourteen'
  else if dNum = 15 then
    result := 'Fifteen'
  else if dNum = 16 then
    result := 'Sixteen'
  else if dNum = 17 then
    result := 'Seventeen'
  else if dNum = 18 then
    result := 'Eighteen'
  else if dNum = 19 then
    result := 'Nineteen'
  else
    result := 'woops!';
end;

function TwentyTo99(dNum: double): string;
var

  BigNum: string;
begin

  if (dNum < 20) or (dNum > 99) then
    raise exception.Create('TwentyTo99: dNum out of range!');

  BigNum := 'woops';

  if dNum >= 90 then
    BigNum := 'Ninety'
  else if dNum >= 80 then
    BigNum := 'Eighty'
  else if dNum >= 70 then
    BigNum := 'Seventy'
  else if dNum >= 60 then
    BigNum := 'Sixty'
  else if dNum >= 50 then
    BigNum := 'Fifty'
  else if dNum >= 40 then
    BigNum := 'Forty'
  else if dNum >= 30 then
    BigNum := 'Thirty'
  else if 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;
begin

  if 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 then
    raise Exception.Create('Negative numbers not supported');

  if dNum > 999999999.99 then
    raise
      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'
  else if 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's
  if (dNum >= 1000000) and (dNum <= 999999999) then
  begin
    workVar := dNum / 1000000;
    workVar := Int(workVar);
    if (workVar <= 9) then
      result := ZeroTo19(workVar)
    else if (workVar <= 99) then
      result := LessThan99(workVar)
    else if (workVar <= 999) then
      result := Hundreds(workVar)
    else
      result := 'mill fubar';

    result := result + ' Million';

    dNum := dNum - (workVar * 1000000);
  end;

  // deal with 1000's
  if (dNum >= 1000) and (dNum <= 999999.99) then
  begin
    // 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)
    else if (workVar <= 99) then
      result := LessThan99(workVar)
    else if (workVar <= 999) then
      result := Hundreds(workVar)
    else
      result := 'thou fubar';

    result := result + ' Thousand';

    dNum := dNum - (workVar * 1000);
  end;

  // deal with 100's
  if (dNum >= 100.00) and (dNum <= 999.99) then
  begin
    result := result + ' ' + Hundreds(dNum);
    dNum := FloatMod(dNum, 100);
  end;

  // format in anything less than 100
  if (dNum > 0) or ((dNum = 0) and (Length(result) = 0)) then
  begin
    result := result + ' ' + LessThan99(dNum);
  end;
  result := result + ' ' + centsString;
end;

end.





Похожие по теме исходники

Сумма прописью




Copyright © 2004-2024 "Delphi Sources" by BrokenByte Software. Delphi World FAQ

Группа ВКонтакте