Formatted numeric output: Difference between revisions
→{{header|Quackery}}: Tweaked code, added commentary.
(Added Quackery.) |
(→{{header|Quackery}}: Tweaked code, added commentary.) |
||
Line 1,914:
=={{header|Quackery}}==
Behaviour of <code>format ( $ a b --> $ )</code>
[ over find swap found ] is has ( $ c --> b )▼
Accepts a well formatted numerical string <code>$</code>, optionally with a decimal point, without validation.
<code>a</code> is the number of digits before the decimal point. <code>b</code> is the number of digits after the decimal point.
Returns a string <code>$</code> with either a leading zero or a leading space, then ''at least'' <code>a</code> digits, then a decimal point, then ''at least'' <code>b</code> digits. The digits are padded with zeroes fore and aft if required.
"''at least''" – if there are already more digits before the point than <code>a</code>, and/or more digits after the point than <code>b</code>, it will ''not'' crop the number. IMO, it is better to mess up the formatting than to display an incorrect number. Check the $ is not too long fore or aft before passing to <code>format</code> if the string may be truncated.
▲<syntaxhighlight lang="Quackery"> [ over find swap found ] is has ( $ c --> b )
[ over 0 peek
char - = iff
[
true unrot ]
else [ false unrot ]
Line 1,926 ⟶ 1,934:
- char 0 swap of
swap join
swap
else space
swap join ]
[ over char . has not if
Line 1,955 ⟶ 1,964:
{{out}}
<pre> 00007.125
-
00000.125
-
00007.120
-
00000.120
-
00007.000
-
00000.000</pre>
=={{header|R}}==
|