Recent

Author Topic: How to program numbers into words in Pascal?  (Read 712 times)

Jannik004

  • Newbie
  • Posts: 2
How to program numbers into words in Pascal?
« on: June 20, 2022, 04:42:36 pm »
Hello,
I have to build a program with Pascal where numbers need to be written into word
For example: 25 to twenty-five
The limit is 999999.

I think arrays are a good way but how can I do that?
« Last Edit: July 01, 2022, 09:40:51 am by Jannik004 »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 10175
  • FPC developer.
Re: How to program numbers into words in Pascal?
« Reply #1 on: June 20, 2022, 04:47:19 pm »
I think the best is not the code, but to start formulating the rules to change numbers into words.

E.g.  hundreds tens singles, but there are exceptions like eleven which is not ten one  (while 21 is twenty one). And count yourself lucky that English is still quite regular.

rvk

  • Hero Member
  • *****
  • Posts: 4821
Re: How to program numbers into words in Pascal?
« Reply #2 on: June 20, 2022, 05:24:45 pm »
Look here for inspiration (or exact code  :P)

https://rosettacode.org/wiki/Number_names

(Don't submit this as homework   ;D )
« Last Edit: June 20, 2022, 05:27:25 pm by rvk »

dseligo

  • Hero Member
  • *****
  • Posts: 651
Re: How to program numbers into words in Pascal?
« Reply #3 on: June 20, 2022, 07:38:54 pm »
I think I have it in Croatian, but that probably won't work for you.

Handoko

  • Hero Member
  • *****
  • Posts: 4690
  • My goal: build my own game engine using Lazarus
Re: How to program numbers into words in Pascal?
« Reply #4 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  [Select][+][-]
  1. function Terbilang (S : string) : string;
  2. const
  3.   Angka : array ['0'..'9'] of string [9] =
  4.           ('', 'satu ', 'dua ', 'tiga ', 'empat ', 'lima ', 'enam ',
  5.           'tujuh ', 'delapan ', 'sembilan ');
  6. var
  7.   Hasil : string [108];
  8.   Z     : string [8];
  9. begin
  10.   if (S = '0') then begin
  11.     Terbilang := 'Nol.';
  12.     exit
  13.     end;
  14.   if (S [1] = '-') then begin
  15.     Terbilang := '( Negatif ).';
  16.     exit
  17.     end;
  18.   Z := S;
  19.   while (Z [0] < #8) do Z := '0' + Z;
  20.     case (Z [1]) of
  21.       '0' : if (Z [2] > '0') then Hasil := Angka [Z [2]] + 'juta '
  22.               else
  23.                Hasil [0] := #0;
  24.       '1' : case (Z [2]) of
  25.               '0' : Hasil := 'sepuluh juta ';
  26.               '1' : Hasil := 'sebelas juta '
  27.               else
  28.                 Hasil := Angka [Z [2]] + 'belas juta '
  29.               end
  30.       else
  31.         Hasil := Angka [Z [1]] + 'puluh ' + Angka [Z [2]] + 'juta '
  32.       end;
  33.     case (Z [3]) of
  34.       '0' : ;
  35.       '1' : Hasil := Hasil + 'seratus '
  36.       else
  37.         Hasil := Hasil + Angka [Z [3]] + 'ratus '
  38.       end;
  39.     case (Z [4]) of
  40.       '0' : if ((Z [3] > '0') or (Z [4] > '0')) then
  41.               Hasil := Hasil + Angka [Z [5]] + 'ribu '
  42.               else
  43.                 if (Z [5] = '1') then Hasil := Hasil + 'seribu '
  44.                   else
  45.                     if (Z [5] > '1') then
  46.                       Hasil := Hasil + Angka [Z [5]] + 'ribu ';
  47.       '1' : case (Z [5]) of
  48.               '0' : Hasil := Hasil + 'sepuluh ribu ';
  49.               '1' : Hasil := Hasil + 'sebelas ribu '
  50.               else
  51.                 Hasil := Hasil + Angka [Z [5]] + 'belas ribu '
  52.               end
  53.       else
  54.         Hasil := Hasil + Angka [Z [4]] + 'puluh ' + Angka [Z [5]] + 'ribu '
  55.       end;
  56.     case (Z [6]) of
  57.       '0' : ;
  58.       '1' : Hasil := Hasil + 'seratus '
  59.       else
  60.         Hasil := Hasil + Angka [Z [6]] + 'ratus '
  61.       end;
  62.     case (Z [7]) of
  63.       '0' : Hasil := Hasil + Angka [Z [8]];
  64.       '1' : case (Z [8]) of
  65.               '0' : Hasil := Hasil + 'sepuluh ';
  66.               '1' : Hasil := Hasil + 'sebelas '
  67.               else
  68.                 Hasil := Hasil + Angka [Z [8]] + 'belas '
  69.               end
  70.       else
  71.         Hasil := Hasil + Angka [Z [7]] + 'puluh ' + Angka [Z [8]]
  72.       end;
  73.     Terbilang := Hasil + 'rupiah.'
  74. end;

Thaddy

  • Hero Member
  • *****
  • Posts: 11774
Re: How to program numbers into words in Pascal?
« Reply #5 on: June 20, 2022, 10:07:37 pm »
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})
« Last Edit: June 20, 2022, 10:12:51 pm by Thaddy »
Black themes should be banned.

Jannik004

  • Newbie
  • Posts: 2
Re: How to program numbers into words in Pascal?
« Reply #6 on: June 24, 2022, 07:54:03 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  [Select][+][-]
  1. function Terbilang (S : string) : string;
  2. const
  3.   Angka : array ['0'..'9'] of string [9] =
  4.           ('', 'satu ', 'dua ', 'tiga ', 'empat ', 'lima ', 'enam ',
  5.           'tujuh ', 'delapan ', 'sembilan ');
  6. var
  7.   Hasil : string [108];
  8.   Z     : string [8];
  9. begin
  10.   if (S = '0') then begin
  11.     Terbilang := 'Nol.';
  12.     exit
  13.     end;
  14.   if (S [1] = '-') then begin
  15.     Terbilang := '( Negatif ).';
  16.     exit
  17.     end;
  18.   Z := S;
  19.   while (Z [0] < #8) do Z := '0' + Z;
  20.     case (Z [1]) of
  21.       '0' : if (Z [2] > '0') then Hasil := Angka [Z [2]] + 'juta '
  22.               else
  23.                Hasil [0] := #0;
  24.       '1' : case (Z [2]) of
  25.               '0' : Hasil := 'sepuluh juta ';
  26.               '1' : Hasil := 'sebelas juta '
  27.               else
  28.                 Hasil := Angka [Z [2]] + 'belas juta '
  29.               end
  30.       else
  31.         Hasil := Angka [Z [1]] + 'puluh ' + Angka [Z [2]] + 'juta '
  32.       end;
  33.     case (Z [3]) of
  34.       '0' : ;
  35.       '1' : Hasil := Hasil + 'seratus '
  36.       else
  37.         Hasil := Hasil + Angka [Z [3]] + 'ratus '
  38.       end;
  39.     case (Z [4]) of
  40.       '0' : if ((Z [3] > '0') or (Z [4] > '0')) then
  41.               Hasil := Hasil + Angka [Z [5]] + 'ribu '
  42.               else
  43.                 if (Z [5] = '1') then Hasil := Hasil + 'seribu '
  44.                   else
  45.                     if (Z [5] > '1') then
  46.                       Hasil := Hasil + Angka [Z [5]] + 'ribu ';
  47.       '1' : case (Z [5]) of
  48.               '0' : Hasil := Hasil + 'sepuluh ribu ';
  49.               '1' : Hasil := Hasil + 'sebelas ribu '
  50.               else
  51.                 Hasil := Hasil + Angka [Z [5]] + 'belas ribu '
  52.               end
  53.       else
  54.         Hasil := Hasil + Angka [Z [4]] + 'puluh ' + Angka [Z [5]] + 'ribu '
  55.       end;
  56.     case (Z [6]) of
  57.       '0' : ;
  58.       '1' : Hasil := Hasil + 'seratus '
  59.       else
  60.         Hasil := Hasil + Angka [Z [6]] + 'ratus '
  61.       end;
  62.     case (Z [7]) of
  63.       '0' : Hasil := Hasil + Angka [Z [8]];
  64.       '1' : case (Z [8]) of
  65.               '0' : Hasil := Hasil + 'sepuluh ';
  66.               '1' : Hasil := Hasil + 'sebelas '
  67.               else
  68.                 Hasil := Hasil + Angka [Z [8]] + 'belas '
  69.               end
  70.       else
  71.         Hasil := Hasil + Angka [Z [7]] + 'puluh ' + Angka [Z [8]]
  72.       end;
  73.     Terbilang := Hasil + 'rupiah.'
  74. end;

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

  • Hero Member
  • *****
  • Posts: 4690
  • My goal: build my own game engine using Lazarus
Re: How to program numbers into words in Pascal?
« Reply #7 on: June 27, 2022, 02:15:37 pm »
Here is the demo showing how to use the function:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Edit1: TEdit;
  16.     Label1: TLabel;
  17.     Label2: TLabel;
  18.     Memo1: TMemo;
  19.     procedure Edit1Change(Sender: TObject);
  20.     procedure FormCreate(Sender: TObject);
  21.   end;
  22.  
  23. var
  24.   Form1: TForm1;
  25.  
  26. implementation
  27.  
  28. function Terbilang (S : string) : string; forward;
  29.  
  30. {$R *.lfm}
  31.  
  32. { TForm1 }
  33.  
  34. procedure TForm1.FormCreate(Sender: TObject);
  35. begin
  36.   Constraints.MinHeight := 240;
  37.   Constraints.MinWidth  := 320;
  38.   Label1.Caption        := 'Please provide a value:';
  39.   Label2.Caption        := 'Terbilang: (in Indonesian)';
  40.   Edit1.Text            := '';
  41.   Edit1.Anchors         := [akTop, akLeft, akRight];
  42.   Memo1.Clear;
  43.   Memo1.ReadOnly        := True;
  44.   Memo1.Anchors         := [akTop, akBottom, akLeft, akRight];
  45. end;
  46.  
  47. procedure TForm1.Edit1Change(Sender: TObject);
  48. var
  49.   S: string;
  50.   i: Integer;
  51. begin
  52.   Memo1.Clear;
  53.   S := Trim(Edit1.Text);               // Remove spaces
  54.   if S.IsEmpty then Exit;              // Nothing
  55.   if not(TryStrToInt(S, i)) then Exit; // Invalid integer
  56.   if S.Length > 8 then Exit;           // Maximum support is 8 digits
  57.   Memo1.Text := Terbilang(S);
  58. end;
  59.  
  60. // BEGIN of Terbilang Function
  61. //
  62. //----- This function is to convert integer to Indonesian Rupiah
  63. //----- Written by Handoko, licensed in Public Domain
  64. //----- https://forum.lazarus.freepascal.org/index.php/topic,59685.msg445487
  65. function Terbilang (S : string) : string;
  66. const
  67.   Angka : array ['0'..'9'] of string [9] =
  68.           ('', 'satu ', 'dua ', 'tiga ', 'empat ', 'lima ', 'enam ',
  69.           'tujuh ', 'delapan ', 'sembilan ');
  70. var
  71.   Hasil : string [108];
  72.   Z     : string [8];
  73. begin
  74.   if (S = '0') then begin
  75.     Terbilang := 'Nol.';
  76.     exit
  77.     end;
  78.   if (S [1] = '-') then begin
  79.     Terbilang := '( Negatif ).';
  80.     exit
  81.     end;
  82.   Z := S;
  83.   while (Z [0] < #8) do Z := '0' + Z;
  84.     case (Z [1]) of
  85.       '0' : if (Z [2] > '0') then Hasil := Angka [Z [2]] + 'juta '
  86.               else
  87.                Hasil [0] := #0;
  88.       '1' : case (Z [2]) of
  89.               '0' : Hasil := 'sepuluh juta ';
  90.               '1' : Hasil := 'sebelas juta '
  91.               else
  92.                 Hasil := Angka [Z [2]] + 'belas juta '
  93.               end
  94.       else
  95.         Hasil := Angka [Z [1]] + 'puluh ' + Angka [Z [2]] + 'juta '
  96.       end;
  97.     case (Z [3]) of
  98.       '0' : ;
  99.       '1' : Hasil := Hasil + 'seratus '
  100.       else
  101.         Hasil := Hasil + Angka [Z [3]] + 'ratus '
  102.       end;
  103.     case (Z [4]) of
  104.       '0' : if ((Z [3] > '0') or (Z [4] > '0')) then
  105.               Hasil := Hasil + Angka [Z [5]] + 'ribu '
  106.               else
  107.                 if (Z [5] = '1') then Hasil := Hasil + 'seribu '
  108.                   else
  109.                     if (Z [5] > '1') then
  110.                       Hasil := Hasil + Angka [Z [5]] + 'ribu ';
  111.       '1' : case (Z [5]) of
  112.               '0' : Hasil := Hasil + 'sepuluh ribu ';
  113.               '1' : Hasil := Hasil + 'sebelas ribu '
  114.               else
  115.                 Hasil := Hasil + Angka [Z [5]] + 'belas ribu '
  116.               end
  117.       else
  118.         Hasil := Hasil + Angka [Z [4]] + 'puluh ' + Angka [Z [5]] + 'ribu '
  119.       end;
  120.     case (Z [6]) of
  121.       '0' : ;
  122.       '1' : Hasil := Hasil + 'seratus '
  123.       else
  124.         Hasil := Hasil + Angka [Z [6]] + 'ratus '
  125.       end;
  126.     case (Z [7]) of
  127.       '0' : Hasil := Hasil + Angka [Z [8]];
  128.       '1' : case (Z [8]) of
  129.               '0' : Hasil := Hasil + 'sepuluh ';
  130.               '1' : Hasil := Hasil + 'sebelas '
  131.               else
  132.                 Hasil := Hasil + Angka [Z [8]] + 'belas '
  133.               end
  134.       else
  135.         Hasil := Hasil + Angka [Z [7]] + 'puluh ' + Angka [Z [8]]
  136.       end;
  137.     Terbilang := Hasil + 'rupiah.'
  138. end;
  139. // END of Terbilang Function
  140.  
  141. end.

 

TinyPortal © 2005-2018