delphi代码转c#代码_[Delphi代码]数字转Word

本文介绍了如何将Delphi代码转换为C#,以将数值转换成英文单词表示,支持多种货币格式和最多六位小数,同时也适用于数学数字转换。提供了一段C#代码示例,可用于项目的数值到单词转换功能。
摘要由CSDN通过智能技术生成

delphi代码转c#代码

Objective:

目的:

- This article will help user in how to convert their numeric value become words.

-本文将帮助用户如何将其数值转换为单词。

How to use

如何使用

1. You can copy this code in your Unit as function

1.您可以将此代码复制为功能

2. than you can perform your function by type this code

2.然后您可以通过键入以下代码来执行功能

The Code

代码

function TISupport.NTW(ChkER: boolean; ER: string; D: double): string;
const
  Ones: array[0..9] of string = ('Zero', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine');
  Teens: array[10..19] of string = ('Ten', 'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen');
  Tens: array[2..9] of string = ('Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety');
  Suffix: array[0..5] of string = ('Hundred', 'Thousand', 'Million', 'Billion', 'Trillion', 'Quadrillion');
var RStr, sDec, sFrac: string;
    vFrac: double;
    I, vDec : integer;
    TruncTens, TruncHund, TruncThou, TruncMio, TruncBio, TruncTril, iD: Int64;
    ReadFrac: boolean; 
    function fTensENG(xD: integer): string;
    var BTStr: string;
    begin
       if (xD >= 0) and (xD <= 9) then BTStr := Ones[xD] else
       if (xD >= 10) and (xD <= 19) then BTStr := Teens[xD] else
       if (xD >= 20) and (xD <= 99) then
       begin
          if (StrToInt(RightStr(IntToStr(xD), 1)) = 0) then
             BTStr := Tens[StrToInt(LeftStr(IntToStr(xD), 1))]
          else
             BTStr := Tens[StrToInt(LeftStr(IntToStr(xD), 1))] + ' ' +
                      Ones[StrToInt(RightStr(IntToStr(xD), 1))]
       end;
       Result := BTStr;
    end; 
    function fHundENG(xD: integer): string;
    var BTStr: string;
    begin
       BTStr := Ones[StrToInt(LeftStr(IntToStr(xD), 1))] + ' ' + Suffix[0];
       TruncTens := StrToInt(RightStr(IntToStr(xD), 2));
       if (TruncTens <> 0) then
          BTStr := BTStr + ' and ' + fTensENG(TruncTens);
       Result := BTStr;
    end; 
    function fThouENG(xD: Integer): string;
    var BTStr: string;
    begin
       if (xD >= 1000) and (xD <= 9999) then
       begin
          BTStr := Ones[StrToInt(LeftStr(IntToStr(xD), 1))] + ' ' + Suffix[1];
          TruncHund := StrToInt(RightStr(IntToStr(xD), 3));
          TruncTens := StrToInt(RightStr(IntToStr(xD), 2));
          if (TruncHund <> 0) and (TruncTens = 0) then
             BTStr := BTStr + 'and ' + Ones[StrToInt(LeftStr(IntToStr(xD), 1))] + ' ' + Suffix[0]
          else
          if (TruncHund <> 0) and (TruncTens <> 0) then
             BTStr := BTStr + ', ' + fHundENG(TruncHund);
       end
       else
       if (Trunc(xD) >= 10000) and (Trunc(xD) <= 19999) then
       begin
          BTStr := Teens[StrToInt(LeftStr(IntToStr(xD), 2))] + ' ' + Suffix[1];
          TruncHund := StrToInt(RightStr(IntToStr(xD), 3));
          TruncTens := StrToInt(RightStr(IntToStr(xD), 2));
          if (TruncHund <> 0) and (TruncTens = 0) then
             BTStr := BTStr + ' and ' + Ones[StrToInt(LeftStr(IntToStr(TruncHund), 1))] + ' ' + Suffix[0]
          else
          if (TruncHund <> 0) and (TruncTens <> 0) then
             BTStr := BTStr + ', ' + fHundENG(TruncHund);
       end
       else
       if (Trunc(xD) >= 20000) and (Trunc(xD) <= 99999) then
       begin
          if (StrToInt(MidStr(IntToStr(xD), 2, 1)) = 0) then
             BTStr := Tens[StrToInt(LeftStr(IntToStr(xD), 1))] + ' ' + Suffix[1]
          else
             BTStr := Tens[StrToInt(LeftStr(IntToStr(xD), 1))] + ' ' +
                      Ones[StrToInt(MidStr(IntToStr(xD), 2, 1))] + ' ' + Suffix[1];
          TruncHund := StrToInt(RightStr(IntToStr(xD), 3));
          TruncTens := StrToInt(RightStr(IntToStr(xD), 2));
          if (TruncHund <> 0) and (TruncTens = 0) then
             BTStr := BTStr + 'and ' + Ones[StrToInt(LeftStr(IntToStr(TruncHund), 1))] + ' ' + Suffix[0]
          else
          if (TruncHund <> 0) and (TruncTens <> 0) then
             BTStr := BTStr + ', ' + fHundENG(TruncHund);
       end
       else
       if (xD >= 100000) and (xD <= 9999999) then
       begin
          BTStr := fHundENG(StrToInt(LeftStr(IntToStr(xD), 3))) + ' ' + Suffix[1];
          TruncHund := StrToInt(RightStr(IntToStr(xD), 3));
          TruncTens := StrToInt(RightStr(IntToStr(xD), 2));
          if (TruncHund <> 0) and (TruncTens = 0) then
             BTStr := BTStr + 'and ' + Ones[StrToInt(LeftStr(IntToStr(xD), 1))] + ' ' + Suffix[0]
          else
          if (TruncHund <> 0) and (TruncTens <> 0) then
             BTStr := BTStr + ', ' + fHundENG(TruncHund);
       end;
       Result := BTStr;
    end; 
    function fMioENG(xD: Int64): string;
    var BTStr: string;
    begin
       if (xD >= 1000000) and (xD <= 9999999) then
       begin
          BTStr := Ones[StrToInt(LeftStr(IntToStr(xD), 1))] + ' ' + Suffix[2];
          TruncThou := StrToInt(RightStr(IntToStr(xD), 6));
          if (TruncThou <> 0) then
             BTStr := BTStr + ', ' + fThouENG(TruncThou);
       end
       else
       if (xD >= 10000000) and (xD <= 19999999) then
       begin
          BTStr := Teens[StrToInt(LeftStr(IntToStr(xD), 2))] + ' ' + Suffix[2];
          TruncThou := StrToInt(RightStr(IntToStr(xD), 6));
          if (TruncThou <> 0) then
             BTStr := BTStr + ', ' + fThouENG(TruncThou);
       end
       else
       if (xD >= 20000000) and (xD <= 99999999) then
       begin
          if (StrToInt(LeftStr(IntToStr(xD), 2)) = 0) then
             BTStr := Tens[StrToInt(LeftStr(IntToStr(xD), 2))] + ' ' + Suffix[2]
          else
             BTStr := Tens[StrToInt(LeftStr(IntToStr(xD), 2))] + ' ' +
                      Ones[StrToInt(LeftStr(IntToStr(xD), 2))] + ' ' + Suffix[2];
          TruncThou := StrToInt(RightStr(IntToStr(xD), 6));
          if (TruncThou <> 0) then
             BTStr := BTStr + ', ' + fThouENG(TruncThou);
       end
       else
       if (xD >= 100000000) and (xD <= 999999999) then
       begin
          BTStr := fHundENG(StrToInt(LeftStr(IntToStr(xD), 3))) + ' ' + Suffix[2];
          TruncThou := StrToInt(RightStr(IntToStr(xD), 6));
          if (TruncThou <> 0) then
             BTStr := BTStr + ', ' + fThouENG(TruncThou);
       end
       else
       begin
          TruncThou := StrToInt(RightStr(IntToStr(xD), 6));
          if (TruncThou <> 0) then
             BTStr := BTStr + fThouENG(TruncThou);
       end;
       Result := BTStr;
    end; 
    function fBioENG(xD: Int64): string;
    var BTStr: string;
    begin
       if (xD >= 1000000000) and (xD <= 9999999999) then
       begin
          BTStr := Ones[StrToInt64(LeftStr(IntToStr(xD), 1))] + ' ' + Suffix[3];
          TruncMio := StrToInt64(RightStr(IntToStr(xD), 9));
          if (TruncMio <> 0) then
             BTStr := BTStr + ', ' + fMioENG(TruncMio);
       end
       else
       if (xD >= 10000000000) and (xD <= 19999999999) then
       begin
          BTStr := Teens[StrToInt64(LeftStr(IntToStr(xD), 2))] + ' ' + Suffix[3];
          TruncMio := StrToInt64(RightStr(IntToStr(xD), 9));
          if (TruncMio <> 0) then
             BTStr := BTStr + ', ' + fMioENG(TruncMio);
       end
       else
       if (xD >= 20000000000) and (xD <= 99999999999) then
       begin
          if (StrToInt64(LeftStr(IntToStr(xD), 2)) = 0) then
             BTStr := Tens[StrToInt64(LeftStr(IntToStr(xD), 2))] + ' ' + Suffix[3]
          else
             BTStr := Tens[StrToInt64(LeftStr(IntToStr(xD), 2))] + ' ' +
                      Ones[StrToInt64(LeftStr(IntToStr(xD), 2))] + ' ' + Suffix[3];
          TruncMio := StrToInt64(RightStr(IntToStr(xD), 9));
          if (TruncMio <> 0) then
             BTStr := BTStr + ', ' + fMioENG(TruncMio);
       end
       else
       if (xD >= 100000000000) and (xD <= 999999999999) then
       begin
          BTStr := fHundENG(StrToInt64(LeftStr(IntToStr(xD), 3))) + ' ' + Suffix[3];
          TruncMio := StrToInt64(RightStr(IntToStr(xD), 9));
          if (TruncMio <> 0) then
             BTStr := BTStr + ', ' + fMioENG(TruncMio);
       end
       else
       begin
          TruncMio := StrToInt64(RightStr(IntToStr(xD), 9));
          if (TruncMio <> 0) then
             BTStr := BTStr + fMioENG(TruncMio);
       end;
       Result := BTStr;
    end; 
    function fTrilENG(xD: Int64): string;
    var BTStr: string;
    begin
       if (xD >= 1000000000000) and (xD <= 9999999999999) then
       begin
          BTStr := Ones[StrToInt(LeftStr(IntToStr(xD), 1))] + ' ' + Suffix[4];
          TruncBio := StrToInt64(RightStr(IntToStr(xD), 12));
          if (TruncBio <> 0) then
             BTStr := BTStr + ', ' + fBioENG(TruncBio);
       end
       else
       if (xD >= 10000000000000) and (xD <= 19999999999999) then
       begin
          BTStr := Teens[StrToInt(LeftStr(IntToStr(xD), 2))] + ' ' + Suffix[4];
          TruncBio := StrToInt64(RightStr(IntToStr(xD), 12));
          if (TruncBio <> 0) then
             BTStr := BTStr + ', ' + fBioENG(TruncBio);
       end
       else
       if (xD >= 20000000000000) and (xD <= 99999999999999) then
       begin
          if (StrToInt64(LeftStr(IntToStr(xD), 2)) = 0) then
             BTStr := Tens[StrToInt64(LeftStr(IntToStr(xD), 2))] + ' ' + Suffix[4]
          else
             BTStr := Tens[StrToInt64(LeftStr(IntToStr(xD), 2))] + ' ' +
                      Ones[StrToInt64(LeftStr(IntToStr(xD), 2))] + ' ' + Suffix[4];
          TruncBio := StrToInt64(RightStr(IntToStr(xD), 12));
          if (TruncBio <> 0) then
             BTStr := BTStr + ', ' + fBioENG(TruncBio);
       end
       else
       if (xD >= 100000000000000) and (xD <= 999999999999999) then
       begin
          BTStr := fHundENG(StrToInt64(LeftStr(IntToStr(xD), 3))) + ' ' + Suffix[4];
          TruncBio := StrToInt64(RightStr(IntToStr(xD), 12));
          if (TruncBio <> 0) then
             BTStr := BTStr + ', ' + fBioENG(TruncBio);
       end
       else
       begin
          TruncBio := StrToInt64(RightStr(IntToStr(xD), 12));
          if (TruncBio <> 0) then
             BTStr := BTStr + fBioENG(TruncBio);
       end;
       Result := BTStr;
    end; 
    function fQuadENG(xD: Int64): string;
    var BTStr: string;
    begin
       if (xD >= 1000000000000000) and (xD <= 9999999999999999) then
       begin
          BTStr := Ones[StrToInt(LeftStr(IntToStr(xD), 1))] + ' ' + Suffix[5];
          TruncTril := StrToInt64(RightStr(IntToStr(xD), 15));
          if (TruncTril <> 0) then
             BTStr := BTStr + ', ' + fTrilENG(TruncTril);
       end
       else
       if (xD >= 10000000000000000) and (xD <= 19999999999999999) then
       begin
          BTStr := Teens[StrToInt(LeftStr(IntToStr(xD), 2))] + ' ' + Suffix[5];
          TruncTril := StrToInt64(RightStr(IntToStr(xD), 15));
          if (TruncTril <> 0) then
             BTStr := BTStr + ', ' + fTrilENG(TruncTril);
       end
       else
       if (xD >= 20000000000000000) and (xD <= 99999999999999999) then
       begin
          if (StrToInt64(LeftStr(IntToStr(xD), 2)) = 0) then
             BTStr := Tens[StrToInt64(LeftStr(IntToStr(xD), 2))] + ' ' + Suffix[5]
          else
             BTStr := Tens[StrToInt64(LeftStr(IntToStr(xD), 2))] + ' ' +
                      Ones[StrToInt64(LeftStr(IntToStr(xD), 2))] + ' ' + Suffix[5];
          TruncTril := StrToInt64(RightStr(IntToStr(xD), 15));
          if (TruncTril <> 0) then
             BTStr := BTStr + ', ' + fTrilENG(TruncTril);
       end
       else
       if (xD >= 100000000000000000) and (xD <= 999999999999999999) then
       begin
          BTStr := fHundENG(StrToInt64(LeftStr(IntToStr(xD), 3))) + ' ' + Suffix[5];
          TruncTril := StrToInt64(RightStr(IntToStr(xD), 15));
          if (TruncTril <> 0) then
             BTStr := BTStr + ', ' + fTrilENG(TruncTril);
       end;;
       Result := BTStr;
    end; 
begin
   iD := abs(Trunc(D));
   if (iD >= 0) and (iD <= 99) then RStr := fTensENG(iD) else
   if (iD >= 100) and (iD <= 999) then Rstr := RStr + fHundENG(iD) else
   if (iD >= 1000) and (iD <= 999999) then RStr := RStr + fThouENG(iD) else
   if (iD >= 1000000) and (iD <= 999999999) then RStr := RStr + fMioENG(iD) else
   if (iD >= 1000000000) and (iD <= 999999999999) then RStr := RStr + fBioENG(iD) else
   if (iD >= 1000000000000) and (iD <= 999999999999999) then RStr := RStr + fTrilENG(iD) else
   if (iD >= 1000000000000000) and (iD <= 999999999999999999) then RStr := RStr + fQuadENG(iD); 
   if ChkER then RStr := RStr + ' ' + ER;
   vFrac := Frac(D);
   if (vFrac <> 0) then
   begin
      sDec := FormatFloat('0.000000', vFrac);
      if ChkER then
      begin
         sDec := MidStr(sDec, 3, 2);
         vDec := StrToInt(sDec);
         if (vDec > 0) then RStr := RStr + ' and ' + fTensENG(vDec) + ' cents.';
      end
      else
      begin
         RStr := RStr + ' point ';
         ReadFrac := False;
         sFrac := '';
         for I := Length(sDec) downto 3 do
         begin
            if (sDec[I] <> '0') then ReadFrac := True;
            if ReadFrac then sFrac := Ones[StrToInt(sDec[I])] + ' ' + sFrac;
         end;
         RStr := RStr + sFrac;
      end;
   end;
   Result := RStr;
end;

The Implementation

实施

procedure TForm1.Button1Click(Sender: TObject); //Currency Format
begin
     Memo1.Clear;
     Memo1.Text := NTW(True, 'Dollar', 185012,345020);
     //Result : one hundred and eighty five thousand, and twelve dollar and thirty four cents
end;

procedure TForm1.Button2Click(Sender: TObject); //Math Format
begin
     Memo1.Clear;
     Memo1.Text := NTW(False, '', 185012,345020);
     //Result : one hundred and eighty five thousand, and twelve point three four five zero two
end;

Function Explanation

功能说明

NTW(ChkER: Boolean; ER: String; D: Double): String;

NTW(ChkER:布尔值; ER:字符串; D:双精度):字符串;

ChkER = True for  Using Currency Format, False for math format

对于货币格式,ChkER = True;对于数学格式,ChkER = False

ER = Currency Name eg: 'Dollar'

ER =货币名称,例如:“美元”

D = Numeric Value

D =数值

feature::

特征::

1. Support Multi Currency (Means you can input any currency name you want. ^^ )

1.支持多种货币(表示您可以输入所需的任何货币名称。^^)

2. Support Decimal until 6 Digit

2.支持小数到6位

3. Can be use for Math Number-To-Words Also.

3.也可以用于数学数字到单词。

May be for some advance usage you can modified this code as you need, and the last hope that this code can be really useful for your project.

可能是出于一些先期使用的需要,您可以根据需要修改此代码,最后希望该代码对您的项目确实有用。

翻译自: https://www.experts-exchange.com/articles/1335/Delphi-Code-Number-to-Word.html

delphi代码转c#代码

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值