Topic: how to convert number to letter

how to convert number to letter
exmp:
250.00 (TWO HUNDRED AND FIFTY )

in french

Re: how to convert number to letter

Hello negadi37
Try this :

procedure Form1_Button1_OnClick (Sender: string; var Cancel: boolean);
begin
     if ValidFloat(Form1.Edit1.Text) then
         Form1.Edit2.Text:=ToWords(Form1.Edit1.Value);
end;
JB

Re: how to convert number to letter

jean.brezhonek wrote:

Hello negadi37
Try this :

procedure Form1_Button1_OnClick (Sender: string; var Cancel: boolean);
begin
     if ValidFloat(Form1.Edit1.Text) then
         Form1.Edit2.Text:=ToWords(Form1.Edit1.Value);
end;
JB

its works perfectly thanks but can i change the words to franch language ?

Post's attachments

Attachment icon Capture d’écran 2022-04-20 192224.png 2.25 kb, 31 downloads since 2022-04-20 

Re: how to convert number to letter

Something I found on that function ToWords is the fourty which has to be forty. Here's my converted function from php. Maybe you could do some tweak on the initialization and other function to meet your requirement such as changing to other language.

Goodluck.

Post's attachments

Attachment icon ToWords.zip 552.86 kb, 106 downloads since 2022-04-20 

brian

Re: how to convert number to letter

Hello negadi37, Hello Brian

Tu peux également jeter un oeil sur ce lien :
https://www.experts-exchange.com/articl … -Word.html

Also have a look to this link :
https://www.experts-exchange.com/articl … -Word.html

JB

Re: how to convert number to letter

Hi folks,

I would like to add two cents here about the spelling...

brian.zaballa wrote:

Something I found on that function ToWords is the fourty which has to be forty.

I'm guessing the reason it "has to be" forty is because- fourty is technically not an English word, thus it's not in the English dictionary.


It happens to be one of those English words at play.  There are many exception words like this in English that seem to break any rules, which makes it nearly impossible for nonnative English speakers.  But, that's another topic neutral


I have no idea how this works for French though.

-Josh

"Energy and persistence conquer all things."

Re: how to convert number to letter

In French it looks like this:
En français ca donne ceci:

Post's attachments

Attachment icon Chiffres FR.jpg 12.39 kb, 35 downloads since 2022-04-22 

Destiny

Re: how to convert number to letter

Hi Josh,
I guess you'll get anomalies no matter which language you're using.
Personally, I just let the 'towords' function do the heavy lifting and then alter the odd instances that are incorrect using 'replacestr' in a bit of script.
And use much the same approach if, after having used 'towords', you need tot translate into a different language.
Regards,
Derek.

Post's attachments

Attachment icon towords.zip 336.08 kb, 99 downloads since 2022-04-22 

Re: how to convert number to letter

there is the code to convert number to word in franch
procedure MISSIONCADRE_Button3_OnClick (Sender: TObject; var Cancel: boolean);
begin
    //mission.Label33.Caption := ToWords(mission.Edit23.Value);
    MISSIONCADRE.Label33.Caption := MoneyToWords(MISSIONCADRE.Edit25.Value);
    MISSIONCADRE.Edit23.text := MoneyToWords(MISSIONCADRE.Edit25.Value);
end;

function RoundOff(num:Real; places:integer=2): Real;
begin
    result := StrToFloat(Format('%.'+IntToStr(places)+'f', [num]));
end;

function LPad(PadString : string ; HowMany : integer ; PadValue : string='0'): String;
var
   Counter : integer;
   x : integer;
   NewString : string;
begin
   Counter := HowMany - Length(PadString);
   NewString := '';
   for x := 1 to Counter do
   begin
      NewString := NewString + PadValue;
   end;
   Result := NewString + PadString;
end;

function RPad(PadString : string ; HowMany : integer ; PadValue : string='0'): String;
var
   Counter : integer;
   x : integer;
   NewString : string;
begin
   Counter := HowMany - Length(PadString);
   NewString := '';
   for x := 1 to Counter do
   begin
      NewString := PadValue+NewString;
   end;
   Result := PadString+NewString;
end;

{
    /// https://stackoverflow.com/questions/115 … r-in-words
}
function MoneyToWords(const Number: double; UseCurrency: Boolean = True): String;
var
    _fraction,_i,_maxLen,_levels,_numLen,_cutPos,_hundred,_tens,_currNum,_singles: Integer;
    _strNum,_fr: String;
    _arrStr : Array of String;
    _LIST1 : Array of String =['', 'un', 'deux', 'trois', 'quatre', 'cinq', 'six', 'sept', 'huit', 'neuf', 'dix', 'onze',
            'douze', 'treize', 'quatorze', 'quinze', 'seize', 'dix-sept', 'dix-huit', 'dix-neuf'
        ] ;
    _LIST2 : Array of String = ['', 'dix', 'vingt', 'trente', 'quarante', 'cinquante', 'soixante', 'soixante-dix', 'quatre-vingt', 'quatre-vingt-dix', 'cent'] ;
    _LIST3 : Array of String =['', 'mille', 'million', 'milliard', 'billion', 'quadrillion', 'quintillion', 'sextillion', 'septillion',
            'octillion', 'nonillion', 'decillion', 'undecillion', 'duodecillion', 'tredecillion', 'quattuordecillion',
            'quindecillion', 'sexdecillion', 'septendecillion', 'octodecillion', 'novemdecillion', 'vigintillion'
        ] ;
begin
    result := '';
    _fraction := 0;
    _strNum := VarToStr(Number);
    _strNum := ReplaceStr(_strNum,',','');
    if ValidFloat(_strNum) then begin
        if StrToFloat(_strNum) > 0 then begin
            _strNum := FloatToStr(RoundOff(StrToFloat(_strNum)));
            _arrStr := SplitString(_strNum,'.');

            if Length(_arrStr) > 1 then begin
                _fr := _arrStr[1];
                _fraction := StrToInt(_arrStr[1]);
            end;

            _strNum := _arrStr[0];
            _numLen := Length(_strNum);
            _levels := Int((_numLen + 2) / 3);
            _maxLen := _levels * 3;

            _strNum := LPAD(_strNum, _maxLen);

            for _i:=1 to Int(Length(_strNum)/3) do begin
                Dec(_levels);
                _cutPos := (3*_i)-3;
                if _i>1 then begin
                    Inc(_cutPos);
                end;

                _currNum := StrToInt(Copy(_strNum,_cutPos,3));
                _hundred := Int(_currNum/100);
                if _hundred > 0 then begin
                    result := result + _LIST1[_hundred] + ' cent ';
                end;

                _tens := _currNum mod 100;
                if _tens < 20 then begin
                    result := result + _LIST1[_tens];
                end else begin
                    result := result + _LIST2[Int(_tens/10)];
                    _singles := _currNum mod 10;
                    if _singles > 0 then
                        result := result + ' ' + _LIST1[_singles];
                end;

                if _levels > 0 then
                    if _currNum > 0  then
                        result := result + ' ' + _LIST3[_levels] + ' ';
            end;

            if result[Length(result)]=' ' then
                result :=  Copy(result,0,Length(result)-1);

            if _fraction > 0 then begin
                if Length(_arrStr[1]) > 1 then
                    result := result + ' and ' + LPad(_arrStr[1],2) + '/100'
                else
                    result := result + ' and ' + RPad(_arrStr[1],2) + '/100'
            end;
        end;
    end;
end;

Post's attachments

Attachment icon fr.png 4.41 kb, 31 downloads since 2022-04-23 

Re: how to convert number to letter

derek wrote:

Hi Josh,
I guess you'll get anomalies no matter which language you're using.
Personally, I just let the 'towords' function do the heavy lifting and then alter the odd instances that are incorrect using 'replacestr' in a bit of script.
And use much the same approach if, after having used 'towords', you need tot translate into a different language.
Regards,
Derek.

These are great.  I always like reviewing your examples.  Thanks from me too.

"Energy and persistence conquer all things."