Forum > Beginners

How to program numbers into words in Pascal?

<< < (2/2)

Thaddy:
Nice example. Correct, and it hides the example for the teacher.. ::)
There are only a few forum members that can understand Bahasa Indonesia, let alone teachers  :-X

For OP: the pascal examples from rosetta code - above is the link - compile in FreePascal. Study them and write your own....

If it is not homework, you can simply use those (First example needs {$mode delphi} second probably needs {$mode ISO} or maybe {$mode extendedpascal})

Jannik004:

--- Quote from: Handoko on June 20, 2022, 08:30:25 pm ---Homework? That teacher must be crazy, giving such not beginner-friendly task.

I wrote one, when I worked as an admin, I wrote it for used in a small payroll program I built for the HRD. I even converted it to Excel and used by someone working as a chief accounting. I'm okay to share it here, but I think no one would be interested, it is for Indonesian money.

@Jannik004

Seriously, homework or not, it is a good exercise if you want be a programmer, don't simply copy and use the code you got from other. Analyze, think and understand how it works. There you can learn about parsing and string manipulation.

My solution is don't do it directly from the number as integer but make it a string first, because parsing a string is easier than number (at least that is what I believe). Then process each S[n], which will be ten .. hundred .. thousand and so on, but pay attention on certain n, like the case of eleven, twelve .. as mentioned by marcov.

Here is mine, I put it in Public Domain, hope it can be useful for someone to learn Pascal:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function Terbilang (S : string) : string;const  Angka : array ['0'..'9'] of string [9] =          ('', 'satu ', 'dua ', 'tiga ', 'empat ', 'lima ', 'enam ',          'tujuh ', 'delapan ', 'sembilan ');var  Hasil : string [108];  Z     : string [8];begin  if (S = '0') then begin    Terbilang := 'Nol.';    exit    end;  if (S [1] = '-') then begin    Terbilang := '( Negatif ).';    exit    end;  Z := S;  while (Z [0] < #8) do Z := '0' + Z;    case (Z [1]) of      '0' : if (Z [2] > '0') then Hasil := Angka [Z [2]] + 'juta '              else               Hasil [0] := #0;      '1' : case (Z [2]) of              '0' : Hasil := 'sepuluh juta ';              '1' : Hasil := 'sebelas juta '              else                Hasil := Angka [Z [2]] + 'belas juta '              end      else        Hasil := Angka [Z [1]] + 'puluh ' + Angka [Z [2]] + 'juta '      end;    case (Z [3]) of      '0' : ;      '1' : Hasil := Hasil + 'seratus '      else        Hasil := Hasil + Angka [Z [3]] + 'ratus '      end;    case (Z [4]) of      '0' : if ((Z [3] > '0') or (Z [4] > '0')) then              Hasil := Hasil + Angka [Z [5]] + 'ribu '              else                if (Z [5] = '1') then Hasil := Hasil + 'seribu '                  else                    if (Z [5] > '1') then                      Hasil := Hasil + Angka [Z [5]] + 'ribu ';      '1' : case (Z [5]) of              '0' : Hasil := Hasil + 'sepuluh ribu ';              '1' : Hasil := Hasil + 'sebelas ribu '              else                Hasil := Hasil + Angka [Z [5]] + 'belas ribu '              end      else        Hasil := Hasil + Angka [Z [4]] + 'puluh ' + Angka [Z [5]] + 'ribu '      end;    case (Z [6]) of      '0' : ;      '1' : Hasil := Hasil + 'seratus '      else        Hasil := Hasil + Angka [Z [6]] + 'ratus '      end;    case (Z [7]) of      '0' : Hasil := Hasil + Angka [Z [8]];      '1' : case (Z [8]) of              '0' : Hasil := Hasil + 'sepuluh ';              '1' : Hasil := Hasil + 'sebelas '              else                Hasil := Hasil + Angka [Z [8]] + 'belas '              end      else        Hasil := Hasil + Angka [Z [7]] + 'puluh ' + Angka [Z [8]]      end;    Terbilang := Hasil + 'rupiah.'end;
--- End quote ---

Thank you, you helped me a lot but i don't know how i can use this code in Lazarus now because i don't know how can i link this with the formula.
I left a picture to illustrate the problem.
How can i link this with the "calculate" button and with the input and output window?

Handoko:
Here is the demo showing how to use the function:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, StdCtrls; type   { TForm1 }   TForm1 = class(TForm)    Edit1: TEdit;    Label1: TLabel;    Label2: TLabel;    Memo1: TMemo;    procedure Edit1Change(Sender: TObject);    procedure FormCreate(Sender: TObject);  end; var  Form1: TForm1; implementation function Terbilang (S : string) : string; forward; {$R *.lfm} { TForm1 } procedure TForm1.FormCreate(Sender: TObject);begin  Constraints.MinHeight := 240;  Constraints.MinWidth  := 320;  Label1.Caption        := 'Please provide a value:';  Label2.Caption        := 'Terbilang: (in Indonesian)';  Edit1.Text            := '';  Edit1.Anchors         := [akTop, akLeft, akRight];  Memo1.Clear;  Memo1.ReadOnly        := True;  Memo1.Anchors         := [akTop, akBottom, akLeft, akRight];end; procedure TForm1.Edit1Change(Sender: TObject);var  S: string;  i: Integer;begin  Memo1.Clear;  S := Trim(Edit1.Text);               // Remove spaces  if S.IsEmpty then Exit;              // Nothing  if not(TryStrToInt(S, i)) then Exit; // Invalid integer  if S.Length > 8 then Exit;           // Maximum support is 8 digits  Memo1.Text := Terbilang(S);end; // BEGIN of Terbilang Function////----- This function is to convert integer to Indonesian Rupiah//----- Written by Handoko, licensed in Public Domain//----- https://forum.lazarus.freepascal.org/index.php/topic,59685.msg445487function Terbilang (S : string) : string;const  Angka : array ['0'..'9'] of string [9] =          ('', 'satu ', 'dua ', 'tiga ', 'empat ', 'lima ', 'enam ',          'tujuh ', 'delapan ', 'sembilan ');var  Hasil : string [108];  Z     : string [8];begin  if (S = '0') then begin    Terbilang := 'Nol.';    exit    end;  if (S [1] = '-') then begin    Terbilang := '( Negatif ).';    exit    end;  Z := S;  while (Z [0] < #8) do Z := '0' + Z;    case (Z [1]) of      '0' : if (Z [2] > '0') then Hasil := Angka [Z [2]] + 'juta '              else               Hasil [0] := #0;      '1' : case (Z [2]) of              '0' : Hasil := 'sepuluh juta ';              '1' : Hasil := 'sebelas juta '              else                Hasil := Angka [Z [2]] + 'belas juta '              end      else        Hasil := Angka [Z [1]] + 'puluh ' + Angka [Z [2]] + 'juta '      end;    case (Z [3]) of      '0' : ;      '1' : Hasil := Hasil + 'seratus '      else        Hasil := Hasil + Angka [Z [3]] + 'ratus '      end;    case (Z [4]) of      '0' : if ((Z [3] > '0') or (Z [4] > '0')) then              Hasil := Hasil + Angka [Z [5]] + 'ribu '              else                if (Z [5] = '1') then Hasil := Hasil + 'seribu '                  else                    if (Z [5] > '1') then                      Hasil := Hasil + Angka [Z [5]] + 'ribu ';      '1' : case (Z [5]) of              '0' : Hasil := Hasil + 'sepuluh ribu ';              '1' : Hasil := Hasil + 'sebelas ribu '              else                Hasil := Hasil + Angka [Z [5]] + 'belas ribu '              end      else        Hasil := Hasil + Angka [Z [4]] + 'puluh ' + Angka [Z [5]] + 'ribu '      end;    case (Z [6]) of      '0' : ;      '1' : Hasil := Hasil + 'seratus '      else        Hasil := Hasil + Angka [Z [6]] + 'ratus '      end;    case (Z [7]) of      '0' : Hasil := Hasil + Angka [Z [8]];      '1' : case (Z [8]) of              '0' : Hasil := Hasil + 'sepuluh ';              '1' : Hasil := Hasil + 'sebelas '              else                Hasil := Hasil + Angka [Z [8]] + 'belas '              end      else        Hasil := Hasil + Angka [Z [7]] + 'puluh ' + Angka [Z [8]]      end;    Terbilang := Hasil + 'rupiah.'end;// END of Terbilang Function end.

Navigation

[0] Message Index

[*] Previous page

Go to full version