Formatted numeric output: Difference between revisions

→‎{{header|BASIC}}: Added ANSI BASIC.
(→‎{{header|Quackery}}: Tweaked code, added commentary.)
(→‎{{header|BASIC}}: Added ANSI BASIC.)
 
(2 intermediate revisions by 2 users not shown)
Line 213:
1.9999e01
1.9999e0001
</pre>
 
=={{header|Amazing Hopper}}==
<syntaxhighlight lang="c">
#include <basico.h>
 
#proto padzeros(_X_,_S_,_D_)
#proto padmoney(_N_,_D_,_F_,_S_,_P_)
 
algoritmo
 
n={}, '125, 12.39802, 0.0003221, -0.0045, 457897.789' enlistar en 'n'
 
fijar separador 'NL'
imprimir( "Express a number in decimal as a fixed-length string with leading zeros:\n\n",\
#(pad zeros(n,19,5)), "\n\n",\
#(pad zeros(n,19,0)), "\n\n",\
#(pad zeros( (-9873000155.584),19,1)), "\n\n",\
"Bonus track:\n\n",\
#(lpad( " ",20,string(n))), "\n\n",\
#(lpad( " ",20,notation(n))),"\n\n",\
#(pad money(n,1,".",19,"$")),"\n\n",\
#(pad money(1980.67,1,"_",16,"USD$"),NL))
 
terminar
 
subrutinas
 
pad zeros(n, l, d)
decimales 'd'
s=0, sgn=0
#( s = string( n * sgn:=(sign(n)) ) )
#( sgn = string(sgn) )
si ( s, es matriz? )
mapear( #(sgn=="-1"), "-", sgn )
mapear( #(sgn=="1"), " ", sgn )
sino
#(sgn=="-1"), entonces{ "-",mover a 'sgn' }
#(sgn=="1"), entonces{ " ",mover a 'sgn' }
fin si
#(cat( sgn, lpad( "0",l,s)))
decimales normales
retornar
 
pad money(num, dec, fill, size, prefix)
#(cat(prefix,lpad(fill,size,money(dec,num))))
retornar
</syntaxhighlight>
{{out}}
<pre>
Express a number in decimal as a fixed-length string with leading zeros:
 
0000000000125.00000
0000000000012.39802
0000000000000.00032
-0000000000000.00450
0000000457897.78900
 
0000000000000000125
0000000000000000012
0000000000000000000
-0000000000000000000
0000000000000457898
 
-00000009873000155.6
 
Bonus track:
 
125
12.398020
0.000322
-0.004500
457897.789000
 
1.250000e+02
1.239802e+01
3.221000e-04
-4.500000e-03
4.578978e+05
 
$..............125.0
$...............12.4
$................0.0
$...............-0.0
$..........457,897.8
 
USD$_________1,980.7
 
</pre>
 
Line 334 ⟶ 422:
 
=={{header|BASIC}}==
==={{header|ANSI BASIC}}===
{{works with|Decimal BASIC}}
<syntaxhighlight lang="basic">
100 REM Formatted numeric output
110 LET N = 7.125
120 PRINT USING ("%%%%.###"): N
130 PRINT USING ("****.###"): N
140 PRINT USING ("####.###"): N
150 PRINT USING ("+%%%.###"): N
160 PRINT USING ("+%%%.###"): -N
170 PRINT USING (".###^^^^^"): N
180 PRINT USING ("#.###^^^^^"): N
190 PRINT USING ("##.###^^^^^"): N
200 PRINT USING ("+.###^^^^^"): -N
210 END
</syntaxhighlight>
{{out}}
<pre>
0007.125
***7.125
7.125
+007.125
-007.125
.713E+001
7.125E+000
71.250E-001
-.713E+001
</pre>
 
==={{header|BaCon}}===
BaCon can use C style <tt>printf</tt> format specifiers.
Line 2,458 ⟶ 2,575:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var n = 7.125
511

edits