Home » Archimedes archive » Acorn User » AU 1997-10 A.adf » Extras » Apple][e/PD/BOB/ARMBOB/!ArmBob/progs/h/string/format
Apple][e/PD/BOB/ARMBOB/!ArmBob/progs/h/string/format
This website contains an archive of files for the Acorn Electron, BBC Micro, Acorn Archimedes, Commodore 16 and Commodore 64 computers, which Dominic Ford has rescued from his private collection of floppy disks and cassettes.
Some of these files were originally commercial releases in the 1980s and 1990s, but they are now widely available online. I assume that copyright over them is no longer being asserted. If you own the copyright and would like files to be removed, please contact me.
Tape/disk: | Home » Archimedes archive » Acorn User » AU 1997-10 A.adf » Extras |
Filename: | Apple][e/PD/BOB/ARMBOB/!ArmBob/progs/h/string/format |
Read OK: | ✔ |
File size: | 0651 bytes |
Load address: | 0000 |
Exec address: | 0000 |
File contents
/*------------------------------------------------------------------------- format(n,l,p) formats integers, reals and strings for output. It converts real or integer numbers n to signed decimal strings, with p digits after the decimal point for reals (p is irrelevant for integers and strings), and returns them, or strings n, either padded with leading spaces to make their lengths up to l or, if l is too small (in particular if l = 0), at full length without any leading spaces. GNW 14/05/96 --------------------------------------------------------------------------*/ format(n,l,p) { local d,dec,i,int,neg,r,s; s = ""; switch (typeof(n)) { case REAL: if (neg = (n < 0.0)) n = -n; r = 0.5; for (i = 0; i < p; i++) r /= 10.0; if (n < r) { n = 0.0; neg = FALSE; } else n += r; int = floor(n); if (neg) s = "-"; s += int_10(int); s += "."; dec = n - 1.0*int; for (i = 0; i < p; i ++) dec *= 10.0; dec = floor(dec); d = ""; for (i = 0;i < p; i ++) { d = '0' + dec%10 + d; dec /= 10; } s += d; break; case INTEGER: if (neg = (n < 0)) n = -n; if (neg) s = "-"; s += int_10(n); break; case STRING: s = n; break; default: s = "format: first argument not real, integer or string"; } for (i = l - sizeof(s); i > 0; i --) s = " " + s; return s; } int_10(n) { local s; s = ""; if (n == 0) s = "0"; else while(n) { s = '0' + n%10 + s; n /= 10; } return s; }
00000000 2f 2a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d |/*--------------| 00000010 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d |----------------| * 00000040 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 20 66 6f 72 |-----------. for| 00000050 6d 61 74 28 6e 2c 6c 2c 70 29 20 66 6f 72 6d 61 |mat(n,l,p) forma| 00000060 74 73 20 69 6e 74 65 67 65 72 73 2c 20 72 65 61 |ts integers, rea| 00000070 6c 73 20 61 6e 64 20 73 74 72 69 6e 67 73 20 66 |ls and strings f| 00000080 6f 72 20 6f 75 74 70 75 74 2e 20 20 49 74 20 63 |or output. It c| 00000090 6f 6e 76 65 72 74 73 20 0a 20 72 65 61 6c 20 6f |onverts . real o| 000000a0 72 20 69 6e 74 65 67 65 72 20 6e 75 6d 62 65 72 |r integer number| 000000b0 73 20 6e 20 74 6f 20 73 69 67 6e 65 64 20 64 65 |s n to signed de| 000000c0 63 69 6d 61 6c 20 73 74 72 69 6e 67 73 2c 20 77 |cimal strings, w| 000000d0 69 74 68 20 70 20 64 69 67 69 74 73 20 61 66 74 |ith p digits aft| 000000e0 65 72 0a 20 74 68 65 20 64 65 63 69 6d 61 6c 20 |er. the decimal | 000000f0 70 6f 69 6e 74 20 66 6f 72 20 72 65 61 6c 73 20 |point for reals | 00000100 28 70 20 69 73 20 69 72 72 65 6c 65 76 61 6e 74 |(p is irrelevant| 00000110 20 66 6f 72 20 69 6e 74 65 67 65 72 73 20 61 6e | for integers an| 00000120 64 20 73 74 72 69 6e 67 73 29 2c 0a 20 61 6e 64 |d strings),. and| 00000130 20 72 65 74 75 72 6e 73 20 74 68 65 6d 2c 20 6f | returns them, o| 00000140 72 20 73 74 72 69 6e 67 73 20 6e 2c 20 65 69 74 |r strings n, eit| 00000150 68 65 72 20 70 61 64 64 65 64 20 77 69 74 68 20 |her padded with | 00000160 6c 65 61 64 69 6e 67 20 73 70 61 63 65 73 20 74 |leading spaces t| 00000170 6f 20 6d 61 6b 65 0a 20 74 68 65 69 72 20 6c 65 |o make. their le| 00000180 6e 67 74 68 73 20 75 70 20 74 6f 20 6c 20 6f 72 |ngths up to l or| 00000190 2c 20 69 66 20 6c 20 69 73 20 74 6f 6f 20 73 6d |, if l is too sm| 000001a0 61 6c 6c 20 28 69 6e 20 70 61 72 74 69 63 75 6c |all (in particul| 000001b0 61 72 20 69 66 20 6c 20 3d 20 30 29 2c 20 61 74 |ar if l = 0), at| 000001c0 0a 20 66 75 6c 6c 20 6c 65 6e 67 74 68 20 77 69 |. full length wi| 000001d0 74 68 6f 75 74 20 61 6e 79 20 6c 65 61 64 69 6e |thout any leadin| 000001e0 67 20 73 70 61 63 65 73 2e 20 20 47 4e 57 20 31 |g spaces. GNW 1| 000001f0 34 2f 30 35 2f 39 36 0a 20 2d 2d 2d 2d 2d 2d 2d |4/05/96. -------| 00000200 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d |----------------| * 00000240 2d 2d 2d 2a 2f 0a 0a 66 6f 72 6d 61 74 28 6e 2c |---*/..format(n,| 00000250 6c 2c 70 29 0a 7b 0a 20 20 6c 6f 63 61 6c 20 64 |l,p).{. local d| 00000260 2c 64 65 63 2c 69 2c 69 6e 74 2c 6e 65 67 2c 72 |,dec,i,int,neg,r| 00000270 2c 73 3b 0a 20 20 73 20 3d 20 22 22 3b 0a 20 20 |,s;. s = "";. | 00000280 73 77 69 74 63 68 20 28 74 79 70 65 6f 66 28 6e |switch (typeof(n| 00000290 29 29 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 63 |)). {. c| 000002a0 61 73 65 20 52 45 41 4c 3a 0a 20 20 20 20 20 20 |ase REAL:. | 000002b0 20 20 69 66 20 28 6e 65 67 20 3d 20 28 6e 20 3c | if (neg = (n <| 000002c0 20 30 2e 30 29 29 20 6e 20 3d 20 2d 6e 3b 0a 20 | 0.0)) n = -n;. | 000002d0 20 20 20 20 20 20 20 72 20 3d 20 30 2e 35 3b 0a | r = 0.5;.| 000002e0 20 20 20 20 20 20 20 20 66 6f 72 20 28 69 20 3d | for (i =| 000002f0 20 30 3b 20 69 20 3c 20 70 3b 20 69 2b 2b 29 20 | 0; i < p; i++) | 00000300 72 20 2f 3d 20 31 30 2e 30 3b 0a 20 20 20 20 20 |r /= 10.0;. | 00000310 20 20 20 69 66 20 28 6e 20 3c 20 72 29 20 7b 20 | if (n < r) { | 00000320 6e 20 3d 20 30 2e 30 3b 20 6e 65 67 20 3d 20 46 |n = 0.0; neg = F| 00000330 41 4c 53 45 3b 20 7d 20 65 6c 73 65 20 6e 20 2b |ALSE; } else n +| 00000340 3d 20 72 3b 20 20 20 20 20 20 20 20 0a 20 20 20 |= r; . | 00000350 20 20 20 20 20 69 6e 74 20 3d 20 66 6c 6f 6f 72 | int = floor| 00000360 28 6e 29 3b 0a 20 20 20 20 20 20 20 20 69 66 20 |(n);. if | 00000370 28 6e 65 67 29 20 73 20 3d 20 22 2d 22 3b 0a 20 |(neg) s = "-";. | 00000380 20 20 20 20 20 20 20 73 20 2b 3d 20 69 6e 74 5f | s += int_| 00000390 31 30 28 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 |10(int);. | 000003a0 20 73 20 2b 3d 20 22 2e 22 3b 0a 20 20 20 20 20 | s += ".";. | 000003b0 20 20 20 64 65 63 20 3d 20 6e 20 2d 20 31 2e 30 | dec = n - 1.0| 000003c0 2a 69 6e 74 3b 20 0a 20 20 20 20 20 20 20 20 66 |*int; . f| 000003d0 6f 72 20 28 69 20 3d 20 30 3b 20 69 20 3c 20 70 |or (i = 0; i < p| 000003e0 3b 20 69 20 2b 2b 29 20 64 65 63 20 2a 3d 20 31 |; i ++) dec *= 1| 000003f0 30 2e 30 3b 0a 20 20 20 20 20 20 20 20 64 65 63 |0.0;. dec| 00000400 20 3d 20 66 6c 6f 6f 72 28 64 65 63 29 3b 0a 20 | = floor(dec);. | 00000410 20 20 20 20 20 20 20 64 20 3d 20 22 22 3b 0a 20 | d = "";. | 00000420 20 20 20 20 20 20 20 66 6f 72 20 28 69 20 3d 20 | for (i = | 00000430 30 3b 69 20 3c 20 70 3b 20 69 20 2b 2b 29 0a 20 |0;i < p; i ++). | 00000440 20 20 20 20 20 20 20 20 20 7b 20 64 20 20 3d 20 | { d = | 00000450 27 30 27 20 2b 20 64 65 63 25 31 30 20 2b 20 64 |'0' + dec%10 + d| 00000460 3b 20 64 65 63 20 2f 3d 20 31 30 3b 20 7d 0a 20 |; dec /= 10; }. | 00000470 20 20 20 20 20 20 20 73 20 2b 3d 20 64 3b 0a 20 | s += d;. | 00000480 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 | break;. | 00000490 20 20 20 20 63 61 73 65 20 49 4e 54 45 47 45 52 | case INTEGER| 000004a0 3a 0a 20 20 20 20 20 20 20 20 69 66 20 28 6e 65 |:. if (ne| 000004b0 67 20 3d 20 28 6e 20 3c 20 30 29 29 20 6e 20 3d |g = (n < 0)) n =| 000004c0 20 2d 6e 3b 0a 20 20 20 20 20 20 20 20 69 66 20 | -n;. if | 000004d0 28 6e 65 67 29 20 73 20 3d 20 22 2d 22 3b 0a 20 |(neg) s = "-";. | 000004e0 20 20 20 20 20 20 20 73 20 2b 3d 20 69 6e 74 5f | s += int_| 000004f0 31 30 28 6e 29 3b 0a 20 20 20 20 20 20 20 20 62 |10(n);. b| 00000500 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 |reak;. case| 00000510 20 53 54 52 49 4e 47 3a 0a 20 20 20 20 20 20 20 | STRING:. | 00000520 20 73 20 3d 20 6e 3b 0a 20 20 20 20 20 20 20 20 | s = n;. | 00000530 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 64 65 66 |break;. def| 00000540 61 75 6c 74 3a 0a 20 20 20 20 20 20 20 20 73 20 |ault:. s | 00000550 3d 20 22 66 6f 72 6d 61 74 3a 20 66 69 72 73 74 |= "format: first| 00000560 20 61 72 67 75 6d 65 6e 74 20 6e 6f 74 20 72 65 | argument not re| 00000570 61 6c 2c 20 69 6e 74 65 67 65 72 20 6f 72 20 73 |al, integer or s| 00000580 74 72 69 6e 67 22 3b 20 20 20 20 20 20 20 20 0a |tring"; .| 00000590 20 20 20 20 7d 0a 20 20 66 6f 72 20 28 69 20 3d | }. for (i =| 000005a0 20 6c 20 2d 20 73 69 7a 65 6f 66 28 73 29 3b 20 | l - sizeof(s); | 000005b0 69 20 3e 20 30 3b 20 69 20 2d 2d 29 20 73 20 3d |i > 0; i --) s =| 000005c0 20 22 20 22 20 2b 20 73 3b 0a 20 20 72 65 74 75 | " " + s;. retu| 000005d0 72 6e 20 73 3b 0a 7d 0a 20 0a 69 6e 74 5f 31 30 |rn s;.}. .int_10| 000005e0 28 6e 29 0a 7b 0a 20 20 6c 6f 63 61 6c 20 73 3b |(n).{. local s;| 000005f0 0a 20 20 73 20 3d 20 22 22 3b 0a 20 20 69 66 20 |. s = "";. if | 00000600 28 6e 20 3d 3d 20 30 29 20 73 20 3d 20 22 30 22 |(n == 0) s = "0"| 00000610 3b 0a 20 20 65 6c 73 65 20 77 68 69 6c 65 28 6e |;. else while(n| 00000620 29 20 7b 20 73 20 3d 20 27 30 27 20 2b 20 6e 25 |) { s = '0' + n%| 00000630 31 30 20 2b 20 73 3b 20 6e 20 2f 3d 20 31 30 3b |10 + s; n /= 10;| 00000640 20 7d 0a 20 20 72 65 74 75 72 6e 20 73 3b 0a 7d | }. return s;.}| 00000650 0a |.| 00000651