Formatted numeric output: Difference between revisions

Content added Content deleted
(Added Quackery.)
(→‎{{header|Quackery}}: Tweaked code, added commentary.)
Line 1,914: Line 1,914:
=={{header|Quackery}}==
=={{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
[ over 0 peek
char - = iff
char - = iff
[ 1 -
[ dip [ behead drop ]
dip [ behead drop ]
true unrot ]
true unrot ]
else [ false unrot ]
else [ false unrot ]
Line 1,926: Line 1,934:
- char 0 swap of
- char 0 swap of
swap join
swap join
swap if
swap iff char -
else space
[ char - swap join ] ] is left-pad ( $ n --> $ )
swap join ] is left-pad ( $ n --> $ )


[ over char . has not if
[ over char . has not if
Line 1,955: Line 1,964:
{{out}}
{{out}}


<pre>00007.125
<pre> 00007.125
-0007.125
-00007.125
00000.125
00000.125
-0000.120
-00000.120
00007.120
00007.120
-0007.120
-00007.120
00000.120
00000.120
-0000.120
-00000.120
00007.000
00007.000
-0007.000
-00007.000
00000.000</pre>
00000.000</pre>


=={{header|R}}==
=={{header|R}}==