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>
<syntaxhighlight lang="Quackery">
 
[ 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
[ 1dip -[ behead drop ]
dip [ behead drop ]
true unrot ]
else [ false unrot ]
Line 1,926 ⟶ 1,934:
- char 0 swap of
swap join
swap ififf char -
else space
swap join ] [ char - swap join ] ] is left-pad ( $ n --> $ )
 
[ over char . has not if
Line 1,955 ⟶ 1,964:
{{out}}
 
<pre> 00007.125
-000700007.125
00000.125
-000000000.120
00007.120
-000700007.120
00000.120
-000000000.120
00007.000
-000700007.000
00000.000</pre>
 
=={{header|R}}==
1,462

edits