2005. május 21., szombat

How to convert a currency value into a string


Problem/Question/Abstract:

How can I convert a value like $ 405.00 into a string?

Answer:

Solve 1:

I have written a function similar to what you want, but some modification need to be done. CurrToStrFunc(1234.56, 'Dollar', 'Cent');

unit CurrToStrProc;

interface

uses
  SysUtils;

function CurrToStrFunc(InputCur: Currency; InputDollar: string; InputCent: string):
  string;
function Length_1(InputString: string): string;
function Length_2(InputString: string): string;
function Length_3(InputString: string): string;
function Length_4(InputString: string): string;
function Length_5(InputString: string): string;
function Length_6(InputString: string): string;
function Length_7(InputString: string): string;
function Length_8(InputString: string): string;
function Length_9(InputString: string): string;

implementation

function CurrToStrFunc(InputCur: Currency; InputDollar: string; InputCent: string):
  string;
var
  InputStr, DollarValue, DollarStr, CentValue, CentStr: string;
  Counter, CentCounter: Integer;
  CentFlag: Boolean;
begin
  InputStr := CurrToStr(InputCur);
  DollarValue := '';
  DollarStr := '';
  CentValue := '';
  CentStr := '';
  CentCounter := 0;
  CentFlag := False;
  for Counter := 1 to StrLen(PChar(InputStr)) do
  begin
    if (InputStr[Counter] <> '.') and (CentFlag = False) then
      DollarValue := DollarValue + InputStr[Counter]
    else
    begin
      if (InputStr[Counter] <> '.') then
      begin
        if (CentCounter < 2) then
          CentValue := CentValue + InputStr[Counter];
        CentCounter := CentCounter + 1;
      end;
      CentFlag := True;
    end;
  end;
  if (CentCounter = 1) then
    CentValue := CentValue + '0';
  case StrLen(PChar(DollarValue)) of
    0: DollarStr := '';
    1: DollarStr := Length_1(DollarValue);
    2: DollarStr := Length_2(DollarValue);
    3: DollarStr := Length_3(DollarValue);
    4: DollarStr := Length_4(DollarValue);
    5: DollarStr := Length_5(DollarValue);
    6: DollarStr := Length_6(DollarValue);
    7: DollarStr := Length_7(DollarValue);
    8: DollarStr := Length_8(DollarValue);
    9: DollarStr := Length_9(DollarValue);
  else
    DollarStr := '?';
  end;
  if (CentFlag = True) then
    CentStr := Length_2(CentValue);
  if (InputDollar = '') then
  begin
    if (InputCent = '') then
    begin
      if (CentFlag = True) then
        Result := DollarStr + ' ' + CentStr + ' ' + 'Only'
      else
        Result := DollarStr + ' ' + 'Only';
    end
    else if (CentFlag = True) then
      Result := DollarStr + ' ' + InputCent + ' ' + CentStr + ' ' + 'Only'
    else
      Result := DollarStr + ' ' + 'Only';
  end
  else
  begin
    if (InputCent = '') then
    begin
      if (CentFlag = True) then
        Result := InputDollar + ' ' + DollarStr + ' ' + CentStr + ' ' + 'Only'
      else
        Result := InputDollar + ' ' + DollarStr + ' ' + 'Only';
    end
    else if (CentFlag = True) then
      Result := InputDollar + ' ' + DollarStr + ' ' + InputCent + ' ' + CentStr +
                                 ' ' + 'Only'
    else
      Result := InputDollar + ' ' + DollarStr + ' ' + 'Only';
  end;
end;

function Length_1(InputString: string): string;
begin
  case StrToInt(InputString) of
    1: Result := 'One';
    2: Result := 'Two';
    3: Result := 'Three';
    4: Result := 'Four';
    5: Result := 'Five';
    6: Result := 'Six';
    7: Result := 'Seven';
    8: Result := 'Eight';
    9: Result := 'Nine';
  end;
end;

function Length_2(InputString: string): string;
begin
  case StrToInt(InputString[1]) of
    0:
      begin
        Result := Length_1(InputString[2])
      end;
    1:
      begin
        case StrToInt(InputString[2]) of
          0: Result := 'Ten';
          1: Result := 'Eleven';
          2: Result := 'Twelve';
          3: Result := 'Thirteen';
          4: Result := 'Fourteen';
          5: Result := 'Fiveteen';
          6: Result := 'Sixteen';
          7: Result := 'Seventeen';
          8: Result := 'Eighteen';
          9: Result := 'Nineteen';
        end;
      end;
    2:
      begin
        if (InputString[2] = '0') then
          Result := 'Twenty'
        else
          Result := 'Twenty' + ' ' + Length_1(InputString[2])
      end;
    3:
      begin
        if (InputString[2] = '0') then
          Result := 'Thirty'
        else
          Result := 'Thirty' + ' ' + Length_1(InputString[2])
      end;
    4:
      begin
        if (InputString[2] = '0') then
          Result := 'Fourty'
        else
          Result := 'Fourty' + ' ' + Length_1(InputString[2])
      end;
    5:
      begin
        if (InputString[2] = '0') then
          Result := 'Fivety'
        else
          Result := 'Fivety' + ' ' + Length_1(InputString[2])
      end;
    6:
      begin
        if (InputString[2] = '0') then
          Result := 'Sixty'
        else
          Result := 'Sixty' + ' ' + Length_1(InputString[2])
      end;
    7:
      begin
        if (InputString[2] = '0') then
          Result := 'Seventy'
        else
          Result := 'Seventy' + ' ' + Length_1(InputString[2])
      end;
    8:
      begin
        if (InputString[2] = '0') then
          Result := 'Eighty'
        else
          Result := 'Eighty' + ' ' + Length_1(InputString[2])
      end;
    9:
      begin
        if (InputString[2] = '0') then
          Result := 'Ninety'
        else
          Result := 'Ninety' + ' ' + Length_1(InputString[2])
      end;
  end;
end;

function Length_3(InputString: string): string;
begin
  if (Copy(InputString, 1, 2) = '00') then
    Result := Length_1(InputString[3])
  else if (Copy(InputString, 2, 2) = '00') then
    Result := Length_1(InputString[1]) + ' ' + 'Hundred'
  else if (Copy(InputString, 1, 1) = '0') then
    Result := Length_2(Copy(InputString, 2, 2))
  else
    Result := Length_1(InputString[1]) + ' ' + 'Hundred' + ' ' +
      Length_2(Copy(InputString, 2, 2));
end;

function Length_4(InputString: string): string;
begin
  if (Copy(InputString, 2, 3) = '000') then
    Result := Length_1(InputString[1]) + ' ' + 'Thousand'
  else if (InputString[2] = '0') then
    Result := Length_1(InputString[1]) + ' ' + 'Thousand' + ' ' +
      Length_2(Copy(InputString, 3, 2))
  else
    Result := Length_1(InputString[1]) + ' ' + 'Thousand' + ' ' +
      Length_1(InputString[2]) + ' ' + 'Hundred' + ' ' + Length_2(Copy(InputString, 3,
        2));
end;

function Length_5(InputString: string): string;
begin
  if (Copy(InputString, 2, 4) = '0000') then
    Result := Length_2(Copy(InputString, 1, 2)) + ' ' + 'Thousand'
  else if (InputString[3] = '0') then
    Result := Length_2(Copy(InputString, 1, 2)) + ' ' + 'Thousand' + ' ' +
      Length_2(Copy(InputString, 4, 2))
  else
    Result := Length_2(Copy(InputString, 1, 2)) + ' ' + 'Thousand' + ' ' +
      Length_1(InputString[3]) + ' ' + 'Hundred' + ' ' + Length_2(Copy(InputString, 4,
        2));
end;

function Length_6(InputString: string): string;
begin
  if (Copy(InputString, 1, 3) = '000') then
    Result := Length_3(Copy(InputString, 4, 3))
  else if (Copy(InputString, 2, 5) = '00000') then
    Result := Length_3(Copy(InputString, 1, 3)) + ' ' + 'Thousand'
  else if (InputString[4] = '0') then
    Result := Length_3(Copy(InputString, 1, 3)) + ' ' + 'Thousand' + ' ' +
      Length_2(Copy(InputString, 5, 2))
  else
    Result := Length_3(Copy(InputString, 1, 3)) + ' ' + 'Thousand' + ' ' +
      Length_1(InputString[4]) + ' ' + 'Hundred' + ' ' + Length_2(Copy(InputString, 5,
        2));
end;

function Length_7(InputString: string): string;
begin
  if (Copy(InputString, 2, 6) = '000000') then
    Result := Length_1(InputString[1]) + ' ' + 'Million'
  else if (Copy(InputString, 2, 3) = '000') then
    Result := Length_1(InputString[1]) + ' ' + 'Million' + ' ' +
      Length_3(Copy(InputString, 5, 3))
  else
    Result := Length_1(InputString[1]) + ' ' + 'Million' + ' ' +
      Length_6(Copy(InputString, 2, 6));
end;

function Length_8(InputString: string): string;
begin
  if (Copy(InputString, 2, 7) = '0000000') then
    Result := Length_2(Copy(InputString, 1, 2)) + ' ' + 'Million'
  else if (Copy(InputString, 3, 3) = '000') then
    Result := Length_2(Copy(InputString, 1, 2)) + ' ' + 'Million' + ' ' +
      Length_3(Copy(InputString, 6, 3))
  else
    Result := Length_2(Copy(InputString, 1, 2)) + ' ' + 'Million' + ' ' +
      Length_6(Copy(InputString, 3, 6));
end;

function Length_9(InputString: string): string;
begin
  if (Copy(InputString, 2, 8) = '00000000') then
    Result := Length_3(Copy(InputString, 1, 3)) + ' ' + 'Million'
  else if (Copy(InputString, 7, 3) = '000') then
    Result := Length_3(Copy(InputString, 1, 3)) + ' ' + 'Million' + ' ' +
      Length_3(Copy(InputString, 7, 3))
  else
    Result := Length_3(Copy(InputString, 1, 3)) + ' ' + 'Million' + ' ' +
      Length_6(Copy(InputString, 4, 6));
end;

end.


Solve 2:

function CurrencyToString(const Value: Double): string;
  function IntToEnglish(const Value: LongInt): string;

  implementation

uses
    Math;

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

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

    ENGLISH_GROUP_SUFFIXES: array[1..3] of string = (
      'thousand',
      'million',
      'billion');

  function CurrencyToString(const Value: Double): string;
var
  Cents: LongInt;
begin
  Cents := Round(Value * 100);
  Result := IntToEnglish(Cents div 100) + ' and ' + IntToStr(Cents mod 100) +
    '/100 dollars';
end;

function IntToEnglish(const Value: LongInt): string;
var
  GroupIndex: Integer;
  GroupValue: Integer;
begin
  if (Value = 0) then
    Result := 'zero'
  else if (Value < 0) then
    Result := 'negative ' + IntToEnglish(-Value)
  else
  begin
    Result := '';
    for GroupIndex := (Trunc((8 * SizeOf(Value) - 1) / (3 * Ln(10) / Ln(2)))) downto 0
      do
    begin
      GroupValue := Value div Round(IntPower(10, 3 * GroupIndex)) mod 1000;
      if (GroupValue > 0) then
      begin
        if (GroupValue div 100 > 0) then
          Result := Result + ENGLISH_ONES[GroupValue div 100] + ' hundred';
        case (GroupValue mod 100) of
          0: ;
          1..19:
            Result := Result + ENGLISH_ONES[GroupValue mod 100] + ' ';
        else
          begin
            Result := Result + ENGLISH_TENS[GroupValue div 10 mod 10];
            if (GroupValue mod 10 > 0) then
              Result := Result + '-' + ENGLISH_ONES[GroupValue mod 10];
            Result := Result + ' ';
          end;
        end;
        if (GroupIndex > 0) then
          Result := Result + ENGLISH_GROUP_SUFFIXES[GroupIndex] + ' ';
      end;
    end;
    SetLength(Result, Length(Result) - 1); {Remove the trailing space}
  end;
end;

Calling CurrencyToString(12345678.90) returns: 'twelve million three hundred forty-five thousand six hundred seventy-eight and 90/100 dollars'

Nincsenek megjegyzések:

Megjegyzés küldése