1 (edited by thezimguy 2018-08-14 23:12:04)

Topic: [SOLVED] Figure To Words Function (British Format)

procedure Form1_Edit1onChange(Sender:TObject);
begin
       Form1.Edit2.Text:=ToWords(Form1.Edit1.value);
end;

When I enter 123 or 1234 in Edit1 I get
123:One hundred twenty three
1234:One thousand two hundred thirty four respectively

But what I was expecting to get is
123: One hundred and twenty three
1234: One thousand two hundred and thirty four respectively.


Click on link to get the British Format example
http://myvisualdatabase.com/forum/viewt … 225#p26225

@thezimguy

Re: [SOLVED] Figure To Words Function (British Format)

I created that function in php with the following code

<?php
//echo convert_number(645647);
function convert_number($number){
    if(($number<0)||($number>9999999999)){
        throw new Exception("Number is out of range");
    }
    $Mn=floor($number/1000000);/*millions(giga)*/
    $number-=$Mn*1000000;
    $Gn=floor($number/100000);/*millions(giga)*/
    $number-=$Gn*100000;
    $kn=floor($number/1000);/*Thousands(hecto)*/
    $number-=$kn*1000;
    $Hn=floor($number/100);/*hundreds(hecto)*/
    $number-=$Hn*100;
    $Dn=floor($number/10);/*Tens(deca)*/
    $n=$number%10;/*ones*/
    $res="";
    $z="";
    if($Mn){
        $res.=convert_number($Mn)." Million ";
        $z="mil";//$res.=convert_number($Gn)." Hundred and";
    }
    if($Gn && ($z=="mil")){
      $res.=convert_number($Gn)." Hundred and";
    }
    else if($Gn && ($z=="")){
      $res.=convert_number($Gn)." Hundred ";
    }
    
    if($kn){
        $res.=(empty($res)?"":" ").convert_number($kn)." Thousand ";
    }
    if($Hn){
        $res.=(empty($res)?"":" ").convert_number($Hn)." Hundred ";
    }
    $ones=array("","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen");
    $tens=array("","","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety");
    
    if($Dn||$n){
        if(!empty($res)){
            $res.=" and ";
        }
        if($Dn<2){
            $res.=$ones[$Dn*10+$n];
        }else{
            $res.=$tens[$Dn];
            if($n){
                $res.="-".$ones[$n];
            }
        }
    }
if(empty($res)){
    $res="Zero";
}
return $res;
}
?>
@thezimguy

Re: [SOLVED] Figure To Words Function (British Format)

Sorry, but I can't change this function only for you )
Usually not written word 'and'

Dmitry.

Re: [SOLVED] Figure To Words Function (British Format)

DriveSoft wrote:

Sorry, but I can't change this function only for you )
Usually not written word 'and'

Thank you so much Dmitry.
I really appreciated the time you dedicate to us.

@thezimguy

5 (edited by thezimguy 2018-08-15 07:09:15)

Re: [SOLVED] Figure To Words Function (British Format)

I have created a function to address that for my use.
I think this may be useful to others so find the example project below

http://thezimguy.vacau.com/MVD/toWords.jpg

function NumberToWords(Number: real): string;
var
    Billion: real;
    Million: real;
    Thousand: real;
    Hundred: real;
    Tens : Array[2..9] of string = ['','','Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety'];
    Units: array[1..9] of string = ['','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'];
begin                 
    Billion := 1000000000;
    Million := 1000000;
    Thousand := 1000;
    Hundred := 100;
    if (Number < 0) then Result := 'minus ' else Result := '';
    Number := Abs(Number);
    if (Number >= Billion) then
    begin
        Result := Result + NumberToWords(Number div Billion) + ' billion, ';
        Number := Number mod Billion;
    end;
    if (Number >= Million) then
    begin
        Result:= Result + NumberToWords(Number div Million) + ' million, ';
        Number:= Number mod Million;
    end;
    if (Number >= Thousand) then
    begin
        Result:= Result + NumberToWords(Number div Thousand) + ' thousand, ';
        Number:= Number mod Thousand;
    end;
    if (Number >= Hundred) then
    begin
        Result:= Result + NumberToWords(Number div Hundred) + ' hundred, ';
        Number:= Number mod Hundred;
    end;
    if (Number > 0) and (Result <> '') then
    begin
        Result := Trim(Result);
        if(Result[Length(Result)] = ',') then Delete(Result, Length(Result), 1);
        Result := Result + ' and ';
    end;
    if (Number >= 20) then
    begin
        Result := Result + Tens[StrToInt(FloatToStr(Number)) div 10] + ' ';
        Number := Number mod 10;
    end;
    if(Number >= 10) then
    begin
        Result :=Result + Teens[StrToInt(FloatToStr(Number mod 10))];
        Number := 0;
    end;
    if (Number >= 1) then Result := Result + Units[StrToInt(FloatToStr(Number))];
    Result := Trim(Result);
    if (Result = '') then Result := 'zero'
    else
    if (Result[Length(Result)] = ',') then Delete(Result, Length(Result), 1);
end;

procedure btnMoneyToWord(Sender: string; var Cancel: boolean);
var
    s: string;
    begin
        s:=NumberToWords(Form1.edAmount.Value);
        ShowMessage(s);
    end;

begin
end.

MVD, THE BEST

Post's attachments

Attachment icon toWord2_fixed.zip 336.2 kb, 552 downloads since 2018-08-15 

@thezimguy

Re: [SOLVED] Figure To Words Function (British Format)

Thank you for the example!

Dmitry.

7 (edited by thezimguy 2018-08-15 07:10:16)

Re: [SOLVED] Figure To Words Function (British Format)

There was a little bug, so I have updated the example file

@thezimguy

8 (edited by thezimguy 2018-08-24 17:38:44)

Re: [SOLVED] Figure To Words Function (British Format)

Hello MVD lovers,
I have updated the function for the money. It now includes Decimals and Currency
https://s6.postimg.cc/3yj286qvl/to_Wards2.jpg

Let me know if interested so I can send to you.
Tnx

@thezimguy

9 (edited by thezimguy 2018-08-19 12:00:56)

Re: [SOLVED] Figure To Words Function (British Format)

Hi guy, please is it possible to use {$I filename} or {$INCLUDE filename} or Units in MVD. I want to write seperate scripts and link them in the main script file.
If YES, how can it be done.
Thank you.

@thezimguy

Re: [SOLVED] Figure To Words Function (British Format)

thezimguy wrote:

Hi guy, please is it possible to use {$I filename} or {$INCLUDE filename} or Units in MVD. I want to write seperate scripts and link them in the main script file.
If YES, how can it be done.
Thank you.

You can use keyword "Uses"
Example: http://myvisualdatabase.com/forum/misc. … download=1

Dmitry.

Re: [SOLVED] Figure To Words Function (British Format)

Thank you Dmitry.
You have made my day.
This is really useful.

@thezimguy

Re: [SOLVED] Figure To Words Function (British Format)

Instead of show message how to put the answer in the Edit1.

JUST LEARNING, O GOD HELP ME.

Re: [SOLVED] Figure To Words Function (British Format)

Hi,
Try putting something like this into your script:
procedure Form1_Button1_OnClick (Sender: TObject; var Cancel: boolean);
begin
  showmessage(towords(form1.edit1.value));
end;

procedure Form1_Button2_OnClick (Sender: TObject; var Cancel: boolean);
begin
  form1.edit1.Text := towords(form1.edit1.value);  //*** USD is the default currency - it can be changed as per the example below
//  form1.edit1.text := replacestr(form1.edit1.text,'dollars','pounds');
//  form1.edit1.text := replacestr(form1.edit1.text,'dollar','pounds');
//  form1.edit1.text := replacestr(form1.edit1.text,'cents','pence');
//  form1.edit1.text := replacestr(form1.edit1.text,'cent','pence');
end;

See attached example,
Derek

Post's attachments

Attachment icon message edit1.zip 335.52 kb, 368 downloads since 2019-08-10 

Re: [SOLVED] Figure To Words Function (British Format)

I FIND OUT, FOR WHAT WAS I SEARCHING.
THANKS, DEREK, What I was looking for ...
Instead of Dollar, I wanna want to use MYR.

JUST LEARNING, O GOD HELP ME.

Re: [SOLVED] Figure To Words Function (British Format)

Sorry but I'm unable to change Dollar with MYR, SEN, Please for replacement upload a sample.

JUST LEARNING, O GOD HELP ME.

Re: [SOLVED] Figure To Words Function (British Format)

Try this (see attached).
Derek.

Post's attachments

Attachment icon message edit1a.zip 335.88 kb, 376 downloads since 2019-08-10 

Re: [SOLVED] Figure To Words Function (British Format)

THANKS, DEREK.
OFFS FOR MY BAD LUCK...
I AM NOVOICE BUT FOLLOWING YOU SOON BE BECOME, EXPERT

JUST LEARNING, O GOD HELP ME.

Re: [SOLVED] Figure To Words Function (British Format)

What I'm doing wrong, please help.

Post's attachments

Attachment icon Edit On_Change.zip 2.35 kb, 345 downloads since 2019-08-11 

JUST LEARNING, O GOD HELP ME.

Re: [SOLVED] Figure To Words Function (British Format)

You have changed the name of the procedure in the script to 'registrationpanel' but you haven't changed the name of the edit field to match this (and it should be 'form1.registrationpanel).
I would strongly advise leaving the names of objects (and the generated names of procedures associated with those objects) as the defaults to avoid these sorts of problems.
I have corrected and simplified what you're trying to do (please see the attachment).
Derek.

Post's attachments

Attachment icon editonchange.zip 335.37 kb, 386 downloads since 2019-08-11 

Re: [SOLVED] Figure To Words Function (British Format)

THANKS MASTER DEREK.

JUST LEARNING, O GOD HELP ME.

Re: [SOLVED] Figure To Words Function (British Format)

Hello Derek,
I have adapted your script in my project. In one form it gives perfect results but in another form “rupee ” is spelt as “rupeess”. Can’t figure out the reason. Please help.
Regards,
Eyeman

Post's attachments

Attachment icon NUMBER2WORD.zip 265.38 kb, 202 downloads since 2021-11-06