Formatted numeric output: Difference between revisions

→‎{{header|BASIC}}: Added ANSI BASIC.
(Dialects of BASIC moved to the BASIC section.)
(→‎{{header|BASIC}}: Added ANSI BASIC.)
 
(14 intermediate revisions by 10 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 361 ⟶ 478:
00003.142
-0003.142
</pre>
 
==={{header|BASIC256}}===
<syntaxhighlight lang="basic">n = 7.125
print rjust(string(n), 8, "0") # => 00007.125
print zfill(string(n), 8) # => 00007.125</syntaxhighlight>
 
==={{header|Chipmunk Basic}}===
{{improve|Chipmunk Basic|It does not express numbers with unknown length as a fixed-length string (e.g. 777.125 or 77.125 result in too long string).}}
<syntaxhighlight lang="basic">
10 print using "0000#.###";7.125
</syntaxhighlight>
{{out}}
<pre>
00007.125
</pre>
 
Line 373 ⟶ 505:
 
{{out}}
<pre>00007.125</pre>
00007.125
</pre>
 
==={{header|FutureBasic}}===
Line 384 ⟶ 514:
HandleEvents</syntaxhighlight>
Output:
<pre>00007.125</pre>
00007.125
</pre>
 
==={{header|IS-BASIC}}===
Line 431 ⟶ 559:
Raw number = 0.1007265e-2 Using custom function = 0000000000.00101
</pre>
 
==={{header|Nascom BASIC}}===
{{trans|ZX Spectrum Basic}}
{{works with|Nascom ROM BASIC|4.7}}
<syntaxhighlight lang="basic">
10 REM Formatted numeric output
20 CLS
30 LET N=7.125
40 LET W=9
50 GOSUB 1000
60 SCREEN 10,9:PRINT N$
70 END
1000 REM Formatted fixed-length
1010 N$=STR$(N):N$=RIGHT$(N$,LEN(N$)-1)
1020 FOR I=1 TO W-LEN(N$)
1030 N$="0"+N$
1040 NEXT I
1050 RETURN
</syntaxhighlight>
 
==={{header|PureBasic}}===
Line 436 ⟶ 583:
<syntaxhighlight lang="purebasic">RSet(StrF(7.125,3),8,"0") ; Will be 0007.125</syntaxhighlight>
 
==={{header|QBasic}}===
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
<syntaxhighlight lang="qbasic">n = 7.125
PRINT USING ("0000#.###"); n ' => 00007.125</syntaxhighlight>
 
==={{header|Run BASIC}}===
<syntaxhighlight lang="runbasic">print right$("00000";using("#####.##",7.125),8) ' => 00007.13</syntaxhighlight>
 
 
==={{header|TI-89 BASIC}}===
Line 445 ⟶ 596:
<syntaxhighlight lang="ti89b">right("00000" & format(7.12511, "f3"), 9)</syntaxhighlight>
 
==={{header|True BASIC}}===
<syntaxhighlight lang="qbasic">LET n = 7.125
PRINT USING ("0000#.###"): n ! => 0007.125
END</syntaxhighlight>
 
==={{header|VBA}}===
Line 505 ⟶ 660:
00007.125
</syntaxhighlight>
 
==={{header|XBasic}}===
{{works with|Windows XBasic}}
<syntaxhighlight lang="qbasic">PROGRAM "progname"
VERSION "0.0000"
 
DECLARE FUNCTION Entry ()
 
FUNCTION Entry ()
n! = 7.125
PRINT FORMAT$("0000#.###", n!)
END FUNCTION
END PROGRAM</syntaxhighlight>
{{out}}
<pre>00007.125</pre>
 
==={{header|Yabasic}}===
<syntaxhighlight lang="basic">n = 7.125
print n using ("#####.###") // => 7.125
print str$(n, "%09.3f") // => 00007.125</syntaxhighlight>
 
==={{header|ZX Spectrum Basic}}===
Line 1,196 ⟶ 1,371:
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Formatted_numeric_output}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
In '''[https://formulae.org/?example=Formatted_numeric_output this]''' page you can see the program(s) related to this task and their results.
 
=={{header|Gambas}}==
Line 1,551 ⟶ 1,722:
<syntaxhighlight lang="matlab">>> disp(sprintf('%09.3f',7.125))
00007.125</syntaxhighlight>
 
=={{header|Maxima}}==
{{trans|Common Lisp}}
<syntaxhighlight lang="maxima">
printf(false,"~9,3,,,'0F",7.125);
</syntaxhighlight>
{{out}}
<pre>
"00007.125"
</pre>
 
=={{header|Mercury}}==
Line 1,568 ⟶ 1,749:
 
=={{header|min}}==
{{works with|min|0.1937.60}}
<syntaxhighlight lang="min">(quotepick conslength ""- repeat joinprefix) :str^left-appendpad
(pick length - repeat swap str-append) :left-pad
 
7.125 string "0" 9 left-pad puts!</syntaxhighlight>
{{out}}
<pre>00007.125</pre>
00007.125
</pre>
 
=={{header|Modula-3}}==
Line 1,850 ⟶ 2,028:
e=1.9999e+01 f=19.9991 g=20 !
</pre>
 
=={{header|Quackery}}==
 
Behaviour of <code>format ( $ a b --> $ )</code>
 
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
[ dip [ behead drop ]
true unrot ]
else [ false unrot ]
over char . swap find
- char 0 swap of
swap join
swap iff char -
else space
swap join ] is left-pad ( $ n --> $ )
 
[ over char . has not if
[ dip [ char . join ] ]
over char . swap find
dip [ over size ]
1+ - -
char 0 swap of
join ] is right-pad ( $ n --> $ )
 
[ dip left-pad right-pad ] is format ( $ n n --> $ )
 
' [ $ "7.125"
$ "-7.125"
$ "0.125"
$ "-0.12"
$ "7.12"
$ "-7.12"
$ "0.12"
$ "-0.12"
$ "7"
$ "-7"
$ "0" ]
witheach
[ do 5 3 format echo$ cr ]</syntaxhighlight>
 
{{out}}
 
<pre> 00007.125
-00007.125
00000.125
-00000.120
00007.120
-00007.120
00000.120
-00000.120
00007.000
-00007.000
00000.000</pre>
 
=={{header|R}}==
Line 2,082 ⟶ 2,324:
</syntaxhighlight>
 
=={{header|RPL}}==
Number formatting in RPL is at 1980s standards (ANSI BASIC X3J2, 1983 to be precise). If the user wants something else, she/he has to write some code, formatting the number as a string.
≪ 1 CF '''IF''' OVER 0 < '''THEN''' 1 SF 1 - '''END'''
SWAP ABS →STR
'''WHILE''' DUP2 SIZE > '''REPEAT'''
"0" SWAP + '''END'''
'''IF''' 1 FS? '''THEN''' "-" SWAP + '''END'''
SWAP DROP
≫ ''''TASK'''' STO
{{in}}
<pre>
7.125 9 TASK
-7.125 9 TASK
</pre>
{{out}}
<pre>
2: "00007.125"
1: "-0007.125"
</pre>
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">r = 7.125
Line 2,295 ⟶ 2,556:
<pre>
00007.125
</pre>
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="Zig">
fn main() {
// refer to string interpolation and format place holding in documentation
// pad with zeros towards the left
num := 7.125
println("${num:09f}")
}
</syntaxhighlight>
 
{{out}}
<pre>
00007.125
</pre>
 
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var n = 7.125
511

edits