Forum > Beginners
How to program numbers into words in Pascal?
Jannik004:
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?
marcov:
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:
Look here for inspiration (or exact code :P)
https://rosettacode.org/wiki/Number_names
(Don't submit this as homework ;D )
dseligo:
I think I have it in Croatian, but that probably won't work for you.
Handoko:
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;
Navigation
[0] Message Index
[#] Next page