Formatted numeric output: Difference between revisions

→‎{{header|BASIC}}: Added ANSI BASIC.
(→‎{{header|BASIC}}: Added ANSI BASIC.)
 
(19 intermediate revisions by 14 users not shown)
Line 12:
=={{header|11l}}==
 
<langsyntaxhighlight lang="11l">print(‘#05.3’.format(7.125))</langsyntaxhighlight>
 
{{out}}
Line 23:
 
As an added bonus, this program supports trailing zeroes, but since the example didn't have any it won't get run.
<langsyntaxhighlight lang="asm"> .model small
.stack 1024
.data
Line 68:
 
LeadingZeroes byte 4 ;number of leading zeroes to print
TrailingZeroes byte 0 ;number of trailing zeroes to print</langsyntaxhighlight>
 
{{out}}
Line 78:
 
=={{header|8th}}==
<langsyntaxhighlight lang="forth">
7.125 "%09.3f" s:strfmt
. cr
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 89:
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang="aarch64 assembly">
<lang AArch64 Assembly>
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program formatNum64.s */
Line 137:
qAdrsfNumber1: .quad sfNumber1
 
</syntaxhighlight>
</lang>
{{Output}}
<pre>
Line 145:
 
=={{header|Ada}}==
<langsyntaxhighlight lang="ada">with Ada.Text_Io.Editing; use Ada.Text_Io.Editing;
with Ada.Text_Io; use Ada.Text_Io;
 
Line 158:
begin
Put(Item => Value, Pic => Pic);
end Zero_Fill;</langsyntaxhighlight>
{{out}}
<pre>
Line 165:
 
=={{header|Aime}}==
<langsyntaxhighlight lang="aime">o_form("/w9s0/\n", 7.125);
o_form("/w12d6p6/\n", -12.0625);
o_form("/w12d6p6/\n", 7.125);</langsyntaxhighlight>
{{out}}
<pre>00007.125
Line 180:
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny]}}
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of FORMATted transput}}
<langsyntaxhighlight lang="algol68">main:(
REAL r=exp(pi)-pi;
print((r,newline));
Line 196:
printf(($zzzz-d.ddddeddl$,r));
printf(($4z-d.4de4dl$,r))
)</langsyntaxhighlight>
{{out}}
<pre>
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 219 ⟶ 307:
But to fit the string into a greater container prepending 0 we must write our own function.
(The one here proposed has no a flag for the alignment of the result inside the containing string)
<langsyntaxhighlight lang="amigae">PROC newRealF(es, fl, digit, len=0, zeros=TRUE)
DEF s, t, i
IF (len = 0) OR (len < (digit+3))
Line 237 ⟶ 325:
DEF s[100] : STRING
WriteF('\s\n', newRealF(s, 7.125, 3,9))
ENDPROC</langsyntaxhighlight>
 
=={{header|APL}}==
 
<langsyntaxhighlight lang="apl"> 'ZF15.9' ⎕FMT 7.125
00007.125000000</langsyntaxhighlight>
 
APL's <tt>⎕FMT</tt> is similar to C's <tt>printf</tt> (only it operates on arrays).
Line 248 ⟶ 336:
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
<lang ARM Assembly>
/* ARM assembly Raspberry PI */
/* program formatNum.s */
Line 295 ⟶ 383:
 
 
</syntaxhighlight>
</lang>
 
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">r: 7.125
 
print r
print to :string .format: "09.3f" r</langsyntaxhighlight>
 
{{out}}
Line 311 ⟶ 399:
=={{header|AutoHotkey}}==
contributed by Laszlo on the ahk [http://www.autohotkey.com/forum/post-276467.html#276467 forum]
<langsyntaxhighlight AutoHotkeylang="autohotkey">MsgBox % pad(7.25,7) ; 0007.25
MsgBox % pad(-7.25,7) ; -007.25
 
Line 318 ⟶ 406:
VarSetCapacity(p,len,Asc("0"))
Return SubStr(p x,1-len)
}</langsyntaxhighlight>
 
=={{header|AWK}}==
<langsyntaxhighlight lang="awk">BEGIN {
r=7.125
printf " %9.3f\n",-r
Line 329 ⟶ 417:
printf " %09.3f\n",r
printf " %-09.3f\n",r
}</langsyntaxhighlight>
 
Same output as the C code.
 
=={{header|BaConBASIC}}==
==={{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.
 
<langsyntaxhighlight lang="freebasic">' Formatted numeric output
n = 7.125
PRINT n FORMAT "%09.3f\n"</langsyntaxhighlight>
 
{{out}}
Line 344 ⟶ 462:
00007.125</pre>
 
==={{header|BBC BASIC}}===
<langsyntaxhighlight lang="bbcbasic"> PRINT FNformat(PI, 9, 3)
PRINT FNformat(-PI, 9, 3)
END
Line 355 ⟶ 473:
= RIGHT$(STRING$(sl%,"0") + STR$(n), sl%)
ENDIF
= "-" + RIGHT$(STRING$(sl%,"0") + STR$(-n), sl%-1)</langsyntaxhighlight>
{{out}}
<pre>
Line 361 ⟶ 479:
-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>
 
==={{header|FreeBASIC}}===
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
#Include "vbcompat.bi"
 
Dim s As String = Format(7.125, "00000.0##")
Print s
Sleep</syntaxhighlight>
 
{{out}}
<pre>00007.125</pre>
 
==={{header|FutureBasic}}===
<syntaxhighlight lang="futurebasic">window 1, @"Formatted Numeric Output", (0,0,480,270)
 
print using "0000#.###";7.125
 
HandleEvents</syntaxhighlight>
Output:
<pre>00007.125</pre>
 
==={{header|IS-BASIC}}===
<syntaxhighlight lang="is-basic">100 LET F=7.125
110 PRINT USING "-%%%%%.###":F</syntaxhighlight>
 
==={{header|Liberty BASIC}}===
Custom function builds on the supplied 'print using( "###.###", n)'.<br>
NB no check that this does not truncate high-order digits...
and remember LB calculates with more figures than its normal 'print' displays.
<syntaxhighlight lang="lb">
for i = 1 to 10
n = rnd(1) * 10 ^ (int(10 * rnd(1)) - 2)
print "Raw number = "; n, "Using custom function = "; FormattedPrint$(n, 16, 5)
next i
end
 
function FormattedPrint$(n, length, decPlaces)
format$ = "#."
for i = 1 to decPlaces
format$ = format$ + "#"
next i
 
n$ = using(format$, n) ' remove leading spaces if less than 3 figs left of decimal
' add leading zeros
for i = 1 to len(n$)
c$ = mid$(n$, i, 1)
if c$ = " " or c$ = "%" then nn$ = nn$ + "0" else nn$ = nn$ + c$
next i
FormattedPrint$ = right$( "000000000000" +nn$, length) ' chop to required length
end function
</syntaxhighlight>
{{out}}
<pre>
Raw number = 0.92349667e-2 Using custom function = 0000000000.00923
Raw number = 0.45244905 Using custom function = 0000000000.45245
Raw number = 3084321.68 Using custom function = 0003084321.68220
Raw number = 545.557351 Using custom function = 0000000545.55735
Raw number = 765.455263 Using custom function = 0000000765.45526
Raw number = 650413.092 Using custom function = 0000650413.09248
Raw number = 0.85554345 Using custom function = 0000000000.85554
Raw number = 296.155771 Using custom function = 0000000296.15577
Raw number = 8.3816344 Using custom function = 0000000008.38163
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}}===
Using RSet() to pad 7.125 with 3 decimals converted to a string, to 8 char length.
<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}}===
{{improve|TI-89 BASIC|It does not handle negative numbers.}}
<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}}===
<syntaxhighlight lang="vb">Option Explicit
 
Sub Main()
Debug.Print fFormat(13, 2, 1230.3333)
Debug.Print fFormat(2, 13, 1230.3333)
Debug.Print fFormat(10, 5, 0.3333)
Debug.Print fFormat(13, 2, 1230)
End Sub
 
Private Function fFormat(NbInt As Integer, NbDec As Integer, Nb As Double) As String
'NbInt : Lenght of integral part
'NbDec : Lenght of decimal part
'Nb : decimal on integer number
Dim u As String, v As String, i As Integer
u = CStr(Nb)
i = InStr(u, Application.DecimalSeparator)
If i > 0 Then
v = Mid(u, i + 1)
u = Left(u, i - 1)
fFormat = Right(String(NbInt, "0") & u, NbInt) & Application.DecimalSeparator & Left(v & String(NbDec, "0"), NbDec)
Else
fFormat = Right(String(NbInt, "0") & u, NbInt) & Application.DecimalSeparator & String(NbDec, "0")
End If
End Function
</syntaxhighlight>
{{out}}
<pre>0000000001230.33
30.3333000000000
0000000000.33330
0000000001230.00</pre>
 
==={{header|VBScript}}===
{{works with|Windows Script Host|*}}
<syntaxhighlight lang="vbscript">
a = 1234.5678
 
' Round to three decimal places. Groups by default. Output = "1,234.568".
WScript.Echo FormatNumber(a, 3)
 
' Truncate to three decimal places. Output = "1234.567".
WScript.Echo Left(a, InStr(a, ".") + 3)
 
' Round to a whole number. Grouping disabled. Output = "1235".
WScript.Echo FormatNumber(a, 0, , , False)
 
' Use integer portion only and pad with zeroes to fill 8 chars. Output = "00001234".
WScript.Echo Right("00000000" & Int(a), 8)
</syntaxhighlight>
 
==={{header|Visual Basic}}===
{{works with|Visual Basic|VB6 Standard}}
<syntaxhighlight lang="vb">
Debug.Print Format$(7.125, "00000.000")
</syntaxhighlight>
Output (the decimal separator used depends on the system's language settings):
<syntaxhighlight lang="vb">
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}}===
<syntaxhighlight lang="zxbasic">10 LET n=7.125
20 LET width=9
30 GO SUB 1000
40 PRINT AT 10,10;n$
50 STOP
1000 REM Formatted fixed-length
1010 LET n$=STR$ n
1020 FOR i=1 TO width-LEN n$
1030 LET n$="0"+n$
1040 NEXT i
1050 RETURN </syntaxhighlight>
 
=={{header|bc}}==
First define a custom function for numeric output.
 
<langsyntaxhighlight lang="bc">/*
* Print number n, using at least c characters.
*
Line 441 ⟶ 773:
 
scale = s /* Restore original scale. */
}</langsyntaxhighlight>
 
Then use this function to print 7.125 with 9 characters.
 
<langsyntaxhighlight lang="bc">x = 7.125
"Decimal: "; t = p(x, 9); "
"
Line 454 ⟶ 786:
"Binary: "; t = p(x, 1001); "
"
quit</langsyntaxhighlight>
 
{{out}}
Line 462 ⟶ 794:
 
=={{header|Beads}}==
<langsyntaxhighlight Beadslang="beads">beads 1 program 'Formatted numeric output'
calc main_init
var num = 7.125
log to_str(num, min:9, zero_pad:Y)</langsyntaxhighlight>
{{out}}
<pre>00007.125</pre>
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
main(){
float r=7.125;
Line 479 ⟶ 811:
printf(" %-09.3f\n",r);
return 0;
}</langsyntaxhighlight>
{{out}}
-7.125
Line 489 ⟶ 821:
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">
class Program
{
Line 510 ⟶ 842:
}
</syntaxhighlight>
</lang>
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <iomanip>
 
Line 520 ⟶ 852:
std::cout << std::setfill('0') << std::setw(9) << std::fixed << std::setprecision(3) << 7.125 << std::endl;
return 0;
}</langsyntaxhighlight>
C++20
<syntaxhighlight lang="cpp">#include <iostream>
#include <format>
int main()
{
std::cout << std::format("{:09.3f}\n", 7.125);
return 0;
}</syntaxhighlight>
 
=={{header|Clojure}}==
 
{{trans|Common Lisp}} Using cl format strings
<langsyntaxhighlight lang="lisp">(cl-format true "~9,3,,,'0F" 7.125)</langsyntaxhighlight>
 
{{trans|java}} Using java format strings
<langsyntaxhighlight lang="lisp">(printf "%09.3f" 7.125) ; format works the same way (without side the effect of printing)</langsyntaxhighlight>
 
=={{header|COBOL}}==
This is actually the easiest kind of numeric output to achieve in COBOL, because it requires no adjustments from the way numbers are stored internally (in fixed-point decimal). Each variable declaration requires a <tt>PIC</tt> or <tt>PICTURE</tt> clause describing the kind of data that will be stored there. In this case, we have <tt>9</tt> (a decimal digit), repeated five times; then <tt>V</tt>, the decimal point (cf. French <i>virgule</i>); and then three more decimal digits. Other terms that can appear in <tt>PICTURE</tt> clauses include <tt>A</tt> (a letter of the alphabet), <tt>X</tt> (a character), and <tt>Z</tt> (a decimal digit to be printed with leading spaces instead of leading zeros).
<langsyntaxhighlight lang="cobol">IDENTIFICATION DIVISION.
PROGRAM-ID. NUMERIC-OUTPUT-PROGRAM.
DATA DIVISION.
Line 541 ⟶ 882:
MOVE 7.125 TO X.
DISPLAY X UPON CONSOLE.
STOP RUN.</langsyntaxhighlight>
{{out}}
<pre>00007.125</pre>
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">(format t "~9,3,,,'0F" 7.125)</langsyntaxhighlight>
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.stdio;
 
void main() {
Line 559 ⟶ 900:
writefln(" %09.3f", r);
writefln(" %-09.3f", r);
}</langsyntaxhighlight>
{{out}}
<pre> -7.125
Line 569 ⟶ 910:
 
=={{header|DBL}}==
<langsyntaxhighlight DBLlang="dbl">D5=7125
A10=D5,'-ZZZZX.XXX' ; 7.125
A10=D5,'-ZZZZX.XXX' [LEFT] ;7.125
Line 580 ⟶ 921:
A10=-D5,'ZZZZX.XXX-' [LEFT] ;7.125-
 
A10=1500055,'ZZZ,ZZX.XX ; 15,000.55</langsyntaxhighlight>
 
=={{header|dc}}==
Line 587 ⟶ 928:
First define a custom function for numeric output.
 
<langsyntaxhighlight lang="dc">[*
* (n) (c) lpx
* Print number n, using at least c characters.
Line 698 ⟶ 1,039:
Lnsz Lcsz Ldsz Lfsz LDsz LFsz LLsz LIsz LPsz
Ls k [Restore variable s. Restore original scale.]sz
]sp</langsyntaxhighlight>
 
Then use this function to print 7.125 with 9 characters:
 
<langsyntaxhighlight lang="dc">7.125 sx
[Decimal: ]P lx 9 lpx [
]P 16 i [Hexadecimal: ]P lx 9 lpx [
]P 2 i [Binary: ]P lx 9 lpx [
]P</langsyntaxhighlight>
 
{{out}}
Line 715 ⟶ 1,056:
=={{header|Delphi}}==
 
<syntaxhighlight lang="delphi">
<lang Delphi>
program FormattedNumericOutput;
 
Line 735 ⟶ 1,076:
Readln;
end.
</syntaxhighlight>
</lang>
 
{{out}}
Line 750 ⟶ 1,091:
{{works with|Eiffel Studio|6.6}}
 
<syntaxhighlight lang="eiffel">
<lang Eiffel>
note
description : "{
Line 818 ⟶ 1,159:
 
end
</syntaxhighlight>
</lang>
 
=={{header|Elixir}}==
 
<langsyntaxhighlight lang="elixir">n = 7.125
:io.fwrite "~f~n", [n]
:io.fwrite "~.3f~n", [n]
Line 835 ⟶ 1,176:
:io.fwrite "~e~n", [n]
:io.fwrite "~12.4e~n", [n]
:io.fwrite "~12.4.0e~n", [n]</langsyntaxhighlight>
 
{{out}}
Line 855 ⟶ 1,196:
 
=={{header|Emacs Lisp}}==
<langsyntaxhighlight Lisplang="lisp">(format "%09.3f" 7.125) ;=> "00007.125"</syntaxhighlight>
=>
"00007.125"</lang>
 
<code>format</code> is similar to C <code>sprintf</code>. See [http://www.gnu.org/software/emacs/manual/html_node/elisp/Formatting-Strings.html GNU Elisp manual on Formatting Strings].
Line 870 ⟶ 1,209:
 
=={{header|ERRE}}==
<langsyntaxhighlight ERRElang="erre">PROGRAM FORMATTED
 
PROCEDURE FORMATTED_PRINT(N,LENGTH,DEC_PLACES->FP$)
Line 910 ⟶ 1,249:
END FOR
 
END PROGRAM</langsyntaxhighlight>
{{out}}
<pre>
Line 926 ⟶ 1,265:
 
=={{header|Euphoria}}==
<langsyntaxhighlight lang="euphoria">constant r = 7.125
printf(1,"%9.3f\n",-r)
printf(1,"%9.3f\n",r)
Line 932 ⟶ 1,271:
printf(1,"%09.3f\n",-r)
printf(1,"%09.3f\n",r)
printf(1,"%-09.3f\n",r)</langsyntaxhighlight>
 
{{out}}
Line 945 ⟶ 1,284:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">printfn "%09.3f" 7.125f</langsyntaxhighlight>
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USE: formatting
7.125 "%09.3f\n" printf</langsyntaxhighlight>
{{out}}
<pre>
Line 957 ⟶ 1,296:
=={{header|Fantom}}==
 
<langsyntaxhighlight lang="fantom">
class Main
{
Line 965 ⟶ 1,304:
}
}
</syntaxhighlight>
</lang>
 
=={{header|Forth}}==
Line 976 ⟶ 1,315:
(The 1- is necessary because #s always generates at least one digit, even if it's zero.)
 
<langsyntaxhighlight lang="forth">\ format 'n' digits of the double word 'd'
: #n ( d n -- d ) 0 ?do # loop ;
 
Line 983 ⟶ 1,322:
 
\ d.0 prints a signed double
: d.0 ( d n -- ) >r tuck dabs <# r> 1- #n #s rot sign #> type ;</langsyntaxhighlight>
 
Usage example:
 
<langsyntaxhighlight lang="forth">Type: 123 s>d 8 ud.0
Result: 00000123 ok
Type: -123 s>d 8 d.0
Result: -00000123 ok</langsyntaxhighlight>
===={{header|Detail}}====
Forth's number formatting words are different than many other languages because they are active code rather than using a pattern string. This small set of seven routines ( >DIGIT <# #> # #S HOLD SIGN ) allow arbitrary number formatting of double precision and single precision numbers. The number is created in a "hold' buffer the output is typically a Forth style stack-string consisting of an address and a length.
Line 1,008 ⟶ 1,347:
{{works with|Fortran|90 and later}}
Using standard data edit descriptors it is only possible to precede Integer data with leading zeros.
<langsyntaxhighlight lang="fortran">INTEGER :: number = 7125
WRITE(*,"(I8.8)") number ! Prints 00007125</langsyntaxhighlight>
===On the other hand===
One can engage in trickery via FORMAT statements, in particular the T format option. Unlike actual tab settings which on a typewriter go to a particular column following, T''n'' means go to column ''n''.
<syntaxhighlight lang="fortran">
<lang Fortran>
INTEGER IV
REAL V
Line 1,020 ⟶ 1,359:
1 FORMAT (F9.3,T1,I5.5)
END
</syntaxhighlight>
</lang>
Output is
00007.125
Line 1,029 ⟶ 1,368:
=={{header|Free Pascal}}==
''See [[#Pascal|Pascal]]''
 
=={{header|FreeBASIC}}==
<lang freebasic>' FB 1.05.0 Win64
 
#Include "vbcompat.bi"
 
Dim s As String = Format(7.125, "00000.0##")
Print s
Sleep</lang>
 
{{out}}
<pre>
00007.125
</pre>
 
=={{header|FutureBasic}}==
<lang futurebasic>
include "ConsoleWindow"
 
print using "0000#.###"; 7.125
</lang>
Output:
<pre>
00007.125
</pre>
 
=={{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}}==
'''[https://gambas-playground.proko.eu/?gist=4f64ab96d0f97df5876b76ab0431b302 Click this link to run this code]'''
<langsyntaxhighlight lang="gambas">Public Sub Main()
 
Print Format("7.125", "00000.000")
 
End</langsyntaxhighlight>
Output:
<pre>
Line 1,076 ⟶ 1,386:
 
=={{header|gnuplot}}==
<langsyntaxhighlight lang="gnuplot">print sprintf("%09.3f", 7.125)</langsyntaxhighlight>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">fmt.Printf("%09.3f", 7.125)</langsyntaxhighlight>
 
=={{header|Groovy}}==
Solution:
<langsyntaxhighlight lang="groovy">printf ("%09.3f", 7.125)</langsyntaxhighlight>
 
{{out}}
Line 1,089 ⟶ 1,399:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Text.Printf
main =
printf "%09.3f" 7.125</langsyntaxhighlight>
 
=={{header|hexiscript}}==
<langsyntaxhighlight lang="hexiscript">fun format n length
let n tostr n
while len n < length
Line 1,102 ⟶ 1,412:
endfun
 
format 7.125 9</langsyntaxhighlight>
 
=={{header|HicEst}}==
<langsyntaxhighlight lang="hicest">WRITE(ClipBoard, Format='i5.5, F4.3') INT(7.125), MOD(7.125, 1) ! 00007.125 </langsyntaxhighlight>
 
=={{header|i}}==
<syntaxhighlight lang="i">
<lang i>
concept FixedLengthFormat(value, length) {
string = text(abs(value))
Line 1,130 ⟶ 1,440:
print(FixedLengthFormat(-d, 9))
}
</syntaxhighlight>
</lang>
 
=={{header|Icon}} and {{header|Unicon}}==
<langsyntaxhighlight Iconlang="icon">link printf
 
procedure main()
Line 1,142 ⟶ 1,452:
write(sprintf(p,r)," <=== sprintf ",p)
}
end</langsyntaxhighlight>
 
{{out}} Abbreviated
Line 1,158 ⟶ 1,468:
[[Category:IDL]]
 
<langsyntaxhighlight lang="idl">n = 7.125
print, n, format='(f08.3)'
;==> 0007.125</langsyntaxhighlight>
 
=={{header|IS-BASIC}}==
<lang IS-BASIC>100 LET F=7.125
110 PRINT USING "-%%%%%.###":F</lang>
 
=={{header|J}}==
<langsyntaxhighlight lang="j"> 'r<0>9.3' (8!:2) 7.125
00007.125</langsyntaxhighlight>
 
[http://www.jsoftware.com/help/dictionary/dx008.htm Documentation on 8!:]
Line 1,175 ⟶ 1,481:
{{works with|Java|1.5+}}
Stealing printf from C/C++:
<langsyntaxhighlight lang="java5">public class Printing{
public static void main(String[] args){
double value = 7.125;
Line 1,181 ⟶ 1,487:
System.out.println(String.format("%09.3f",value));
}
}</langsyntaxhighlight>
{{out}}
<pre>000000007.125
000000007.125</pre>
Using <code>NumberFormat</code>:
<langsyntaxhighlight lang="java5">import java.text.DecimalFormat;
import java.text.NumberFormat;
 
Line 1,206 ⟶ 1,512:
System.out.println(numForm.format(7.135));//rounds to even
}
}</langsyntaxhighlight>
{{out}}
<pre>000000007.125
Line 1,214 ⟶ 1,520:
 
=={{header|JavaScript}}==
<langsyntaxhighlight lang="javascript">var n = 123;
var str = ("00000" + n).slice(-5);
alert(str);</langsyntaxhighlight>
 
or, put in browser URL: javascript:n=123;alert(("00000"+n).slice(-5));
Line 1,226 ⟶ 1,532:
requirements, but no truncation occurs; pp/1 is similar but
is likely to be more useful as the decimal point is aligned if possible.
<langsyntaxhighlight lang="jq">def pp0(width):
tostring
| if width > length then (width - length) * "0" + . else . end;
Line 1,240 ⟶ 1,546:
| index(".") as $ix
| ((if $ix then $s[0:$ix] else $s end) | lpad) + "." +
(if $ix then $s[$ix+1:] | .[0:right] else "" end);</langsyntaxhighlight>
'''Examples''':
<langsyntaxhighlight lang="jq">(1.0, 12.3, 333.333, 1e6) | pp0(10)</langsyntaxhighlight>
produces
<langsyntaxhighlight lang="sh">0000000001
00000012.3
000333.333
0001000000</langsyntaxhighlight>
 
<langsyntaxhighlight lang="jq">(1.0, 12.3, 333.333, 1e6) | pp(4;2)</langsyntaxhighlight>
produces
<langsyntaxhighlight lang="sh">0001.
0012.3
0333.33
1000000.</langsyntaxhighlight>
 
=={{header|Julia}}==
Julia's <tt>@sprintf</tt> macro provides string formatting that is similar to that of the c function of the same name. Though easy to use and efficient, <tt>@sprintf</tt> has limited flexibility, as its format specification must be a string literal, precluding its use in dynamic formatting. Greater flexibility is available via the <tt>Formatting</tt> package, which provides an implementation of Python's format specification mini-language. This solution demonstrates both of these techniques to provide the leading zero padded floating point format suggested in the task description ("%09.3f").
<langsyntaxhighlight Julialang="julia">using Printf
test = [7.125, [rand()*10^rand(0:4) for i in 1:9]]
 
Line 1,273 ⟶ 1,579:
printfmtln(fe, i)
end
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,303 ⟶ 1,609:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.0.5-2
 
fun main(args: Array<String>) {
val num = 7.125
println("%09.3f".format(num))
}</langsyntaxhighlight>
 
{{out}}
Line 1,317 ⟶ 1,623:
=={{header|Lambdatalk}}==
Lambdatalk has no primitive for numeric output. This is a way to define it:
<langsyntaxhighlight lang="scheme">
{def fmt
{def padd {lambda {:n :x} {if {< :n 1} then else :x{padd {- :n 1} :x}}}}
Line 1,348 ⟶ 1,654:
>>>>>>>> -69.990
>>>>>> +4970.430
</syntaxhighlight>
</lang>
 
=={{header|Lasso}}==
<langsyntaxhighlight Lassolang="lasso">7.125 -> asstring(-precision = 3, -padding = 9, -padchar = '0')</langsyntaxhighlight>
{{out}}
<pre>00007.125</pre>
 
=={{header|Liberty BASIC}}==
Custom function builds on the supplied 'print using( "###.###", n)'.<br>
NB no check that this does not truncate high-order digits...
and remember LB calculates with more figures than its normal 'print' displays.
<lang lb>
for i =1 to 10
n =rnd( 1) *10^( int( 10 *rnd(1)) -2)
print "Raw number ="; n; "Using custom function ="; FormattedPrint$( n, 16, 5)
next i
end
 
function FormattedPrint$( n, length, decPlaces)
format$ ="#."
for i =1 to decPlaces
format$ =format$ +"#"
next i
 
n$ =using( format$, n) ' remove leading spaces if less than 3 figs left of decimal
' add leading zeros
for i =1 to len( n$)
c$ =mid$( n$, i, 1)
if c$ =" " or c$ ="%" then nn$ =nn$ +"0" else nn$ =nn$ +c$
next i
FormattedPrint$ =right$( "000000000000" +nn$, length) ' chop to required length
end function
</lang>
{{out}}
<pre>
Raw number =0.16045274 Using custom function =0000000000.16045
Raw number =13221.2247 Using custom function =0000013221.22474
Raw number =738.134167 Using custom function =0000000738.13417
Raw number =5.07495908 Using custom function =0000000005.07496
Raw number =4471738.93 Using custom function =0004471738.92920
Raw number =48.7531874 Using custom function =0000000048.75319
Raw number =0.26086972e-1 Using custom function =0000000000.02609
Raw number =0.86559862 Using custom function =0000000000.86560
Raw number =818579.045 Using custom function =0000818579.04498
Raw number =81.460946 Using custom function =0000000081.46095
</pre>
 
=={{header|Logo}}==
Various collection functions, such as MAP and FILTER,
will work on individual characters of a string when given a word instead of a list.
<langsyntaxhighlight lang="logo">to zpad :num :width :precision
output map [ifelse ? = "| | ["0] [?]] form :num :width :precision
end
print zpad 7.125 9 3 ; 00007.125</langsyntaxhighlight>
 
{{works with|UCB Logo}}
As a debugging feature, you can drop down to [[C]] language printf formatting by giving -1 for the width and a format string for the precision.
<langsyntaxhighlight lang="logo">print form 7.125 -1 "|%09.3f| ; 00007.125</langsyntaxhighlight>
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">function digits(n) return math.floor(math.log(n) / math.log(10))+1 end
function fixedprint(num, digs) --digs = number of digits before decimal point
for i = 1, digs - digits(num) do
Line 1,416 ⟶ 1,682:
end
 
fixedprint(7.125, 5) --> 00007.125</langsyntaxhighlight>
 
 
An easier way to do that would be
 
<syntaxhighlight lang="lua">
<lang Lua>
print(string.format("%09.3d",7.125))
</syntaxhighlight>
</lang>
 
=={{header|M2000 Interpreter}}==
We can use ? as Print
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Print str$(7.125,"00000.000")
</syntaxhighlight>
</lang>
 
=={{header|Maple}}==
<langsyntaxhighlight lang="maple">printf("%f", Pi);
3.141593
printf("%.0f", Pi);
Line 1,447 ⟶ 1,713:
+0003.14
printf("%+0*.*f",8, 2, Pi);
+0003.14</langsyntaxhighlight>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">StringTake["000000" <> ToString[7.125], -9]
00007.125</langsyntaxhighlight>
 
=={{header|MATLAB}} / {{header|Octave}}==
<langsyntaxhighlight MATLABlang="matlab">>> disp(sprintf('%09.3f',7.125))
00007.125</langsyntaxhighlight>
 
=={{header|Maxima}}==
{{trans|Common Lisp}}
<syntaxhighlight lang="maxima">
printf(false,"~9,3,,,'0F",7.125);
</syntaxhighlight>
{{out}}
<pre>
"00007.125"
</pre>
 
=={{header|Mercury}}==
<syntaxhighlight lang="text">
:- module formatted_numeric_output.
:- interface.
Line 1,470 ⟶ 1,746:
main(!IO) :-
io.format("%09.3f\n", [f(7.125)], !IO).
</syntaxhighlight>
</lang>
 
=={{header|min}}==
{{works with|min|0.1937.60}}
<syntaxhighlight lang="min">(pick length - repeat prefix) ^left-pad
<lang min>(quote cons "" join) :str-append
(pick length - repeat swap str-append) :left-pad
 
7.125 string "0" 9 left-pad puts!</langsyntaxhighlight>
{{out}}
<pre>00007.125</pre>
00007.125
</pre>
 
=={{header|Modula-3}}==
Modules <tt>IO</tt> and <tt>Fmt</tt> must be imported before use.
<langsyntaxhighlight lang="modula3">IO.Put(Fmt.Pad("7.125\n", length := 10, padChar := '0'));</langsyntaxhighlight>
 
=={{header|Nanoquery}}==
<langsyntaxhighlight Nanoquerylang="nanoquery">printer = 7.125
println format("%09.3f", printer)</langsyntaxhighlight>
{{out}}
<pre>00007.125</pre>
 
=={{header|NetRexx}}==
<langsyntaxhighlight NetRexxlang="netrexx">/* NetRexx */
 
options replace format comments java crossref savelog symbols binary
Line 1,510 ⟶ 1,783:
 
return
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,519 ⟶ 1,792:
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">import strformat
const r = 7.125
echo r
Line 1,525 ⟶ 1,798:
echo fmt"{r:9.3f}"
echo fmt"{-r:09.3f}"
echo fmt"{r:09.3f}"</langsyntaxhighlight>
{{out}}
<pre>7.125
Line 1,536 ⟶ 1,809:
Module <code>Out</code> must be imported before use.
 
<langsyntaxhighlight lang="oberon2">Out.Real(7.125, 9, 0);</langsyntaxhighlight>
 
=={{header|Objective-C}}==
<langsyntaxhighlight lang="objc">NSLog(@"%09.3f", 7.125);</langsyntaxhighlight>
or
<langsyntaxhighlight lang="objc">NSLog(@"%@", [NSString stringWithFormat:@"%09.3f", 7.125]);</langsyntaxhighlight>
 
=={{header|OCaml}}==
<langsyntaxhighlight lang="ocaml">Printf.printf "%09.3f\n" 7.125</langsyntaxhighlight>
 
=={{header|OpenEdge/Progress}}==
<langsyntaxhighlight Progresslang="progress (OpenEdgeopenedge ABLabl)">MESSAGE
STRING( 7.125, "99999.999" )
VIEW-AS ALERT-BOX.</langsyntaxhighlight>
 
=={{header|Oz}}==
Line 1,557 ⟶ 1,830:
It doesn't seem to be possible to use leading zeros for printing,
so we implement this manually:
<langsyntaxhighlight lang="oz">declare
fun {PrintFloat X Prec}
{Property.put 'print.floatPrecision' Prec}
Line 1,567 ⟶ 1,840:
end
in
{System.showInfo {PrintFloat 7.125 8}}</langsyntaxhighlight>
 
=={{header|PARI/GP}}==
{{works with|PARI/GP|2.4.3 and above}}
<langsyntaxhighlight lang="parigp">printf("%09.4f\n", Pi)</langsyntaxhighlight>
 
=={{header|Pascal}}==
<langsyntaxhighlight lang="pascal">procedure writeInFixedFormat(n: real);
const
wholeNumberPlaces = 5;
Line 1,609 ⟶ 1,882:
// rounded
write(n:0:fractionalPlaces);
end;</langsyntaxhighlight>
 
=={{header|Perl}}==
{{works with|Perl|5.x}}
<langsyntaxhighlight lang="perl">printf "%09.3f\n", 7.125;</langsyntaxhighlight>
 
=={{header|Phix}}==
{{libheader|Phix/basics}}
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #7060A8;">printf<span style="color: #0000FF;">(<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #008000;">"%09.3f\n"<span style="color: #0000FF;">,<span style="color: #000000;">7.125<span style="color: #0000FF;">)
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,626 ⟶ 1,899:
 
=={{header|PHP}}==
<langsyntaxhighlight lang="php">echo str_pad(7.125, 9, '0', STR_PAD_LEFT);</langsyntaxhighlight>
or
<langsyntaxhighlight lang="php">printf("%09.3f\n", 7.125);</langsyntaxhighlight>
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(pad 9 (format 7125 3))
(pad 9 (format 7125 3 ",")) # European format</langsyntaxhighlight>
 
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">
<lang PL/I>
put edit (X) (p'999999.V999'); /* Western format. */
 
put edit (X) (p'999999,V999'); /* In European format. */
 
</syntaxhighlight>
</lang>
 
<syntaxhighlight lang="text"> lz: Proc Options(main);
/*********************************************************************
* 10.09.2013 Walter Pachl one way to treat negative numbers
Line 1,666 ⟶ 1,939:
Put Skip List(z,s);
End;
End;</langsyntaxhighlight>
{{out}}
<pre>
Line 1,679 ⟶ 1,952:
The task is underspecified, so we present a few alternatives.
 
<langsyntaxhighlight lang="pop11">;;; field of length 12, 3 digits after decimal place
format_print('~12,3,0,`*,`0F', [1299.19]);
;;; prints "00001299.190"
Line 1,697 ⟶ 1,970:
;;; prints "100000000000000000.000"
;;; that is uses more space if the number does not fit into
;;; fixed width</langsyntaxhighlight>
 
=={{header|PowerShell}}==
Using the <code>-f</code> formatting operator and a custom format string:
<langsyntaxhighlight lang="powershell">'{0:00000.000}' -f 7.125</langsyntaxhighlight>
or by invoking <code>ToString</code> on the number:
<langsyntaxhighlight lang="powershell">7.125.ToString('00000.000')</langsyntaxhighlight>
 
=={{header|PureBasic}}==
Using RSet() to pad 7.125 with 3 decimals converted to a string, to 8 char length.
<lang PureBasic>RSet(StrF(7.125,3),8,"0") ; Will be 0007.125</lang>
 
=={{header|Python}}==
Line 1,715 ⟶ 1,984:
specifying how many digits appear in the exponent when printed with a format.
 
<langsyntaxhighlight lang="python">from math import pi, exp
r = exp(pi)-pi
print r
Line 1,724 ⟶ 1,993:
print "e=%09.4e f=%09.4f g=%09.4g!"%(-r,-r,-r)
print "e=%09.4e f=%09.4f g=%09.4g!"%(r,r,r)
print "e=%-09.4e f=%-09.4f g=%-09.4g!"%(r,r,r)</langsyntaxhighlight>
{{out}}
<pre>
Line 1,738 ⟶ 2,007:
 
{{works with|Python|3}}
<langsyntaxhighlight lang="python">from math import pi, exp
r = exp(pi)-pi
print(r)
Line 1,747 ⟶ 2,016:
print("e={0:09.4e} f={0:09.4f} g={0:09.4g}!".format(-r))
print("e={0:09.4e} f={0:09.4f} g={0:09.4g}!".format(r))
print("e={0:-09.4e} f={0:-09.4f} g={0:-09.4g}!".format(r))</langsyntaxhighlight>
{{out}}
<pre>
Line 1,759 ⟶ 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 1,764 ⟶ 2,097:
[http://www.stat.ucl.ac.be/ISdidactique/Rhelp/library/base/html/sprintf.html sprintf] brings the printf goodness one expects:
 
<langsyntaxhighlight Rlang="r">> sprintf("%f", pi)
[1] "3.141593"
> sprintf("%.3f", pi)
Line 1,791 ⟶ 2,124:
[1] "3141592.65"
> sprintf("%G", 1e-6 * pi)
[1] "3.14159E-06"</langsyntaxhighlight>
 
formatC also provides C-style string formatting.
<langsyntaxhighlight Rlang="r">formatC(x, width=9, flag="0")
# "00007.125"</langsyntaxhighlight>
 
Other string formatting functions include
Line 1,802 ⟶ 2,135:
=={{header|Racket}}==
 
<syntaxhighlight lang="racket">
<lang Racket>
-> (displayln (~a 7.125 #:width 9 #:align 'right #:pad-string "0"))
00007.125
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" perl6line>say 7.125.fmt('%09.3f');</langsyntaxhighlight>
 
=={{header|Raven}}==
<langsyntaxhighlight lang="raven">7.125 "%09.3f" print
 
00007.125</langsyntaxhighlight>
{{trans|Python}}
<langsyntaxhighlight lang="raven">define PI
-1 acos
Line 1,828 ⟶ 2,161:
r -1 * dup dup "e=%09.4e f=%09.4f g=%09.4g!\n" print
r dup dup "e=%09.4e f=%09.4f g=%09.4g!\n" print
r dup dup "e=%-09.4e f=%-09.4f g=%-09.4g!\n" print</langsyntaxhighlight>
<langsyntaxhighlight lang="raven">19.9991
s=19.999100!
e=1.999910e+01 f=19.999100 g=19.9991 G=19.9991!
Line 1,837 ⟶ 2,170:
e=-1.9999e+01 f=-019.9991 g=-00000020!
e=1.9999e+01 f=0019.9991 g=000000020!
e=1.9999e+01 f=19.9991 g=20 !</langsyntaxhighlight>
 
=={{header|REBOL}}==
<langsyntaxhighlight REBOLlang="rebol">REBOL [
Title: "Formatted Numeric Output"
URL: http://rosettacode.org/wiki/Formatted_Numeric_Output
Line 1,875 ⟶ 2,208:
print [zeropad 9 negate 7.125]
print [zeropad 9 7.125]
print 7.125</langsyntaxhighlight>
 
{{out}}
Line 1,886 ⟶ 2,219:
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX program shows various ways to add leading zeroes to numbers. */
a=7.125
b=translate(format(a,10),0,' ')
Line 1,945 ⟶ 2,278:
say 't=' t
say 'u=' u
/*stick a fork in it, we're done.*/</langsyntaxhighlight>
{{out}}
<pre style="height:30ex">
Line 1,980 ⟶ 2,313:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
decimals(3)
see fixedprint(7.125, 5) + nl
Line 1,989 ⟶ 2,322:
next
see num + nl
</syntaxhighlight>
</lang>
 
=={{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}}==
<langsyntaxhighlight lang="ruby">r = 7.125
printf " %9.3f\n", r #=> 7.125
printf " %09.3f\n", r #=> 00007.125
Line 2,000 ⟶ 2,352:
puts " %09.3f" % r #=> 00007.125
puts " %09.3f" % -r #=> -0007.125
puts " %+09.3f" % r #=> +0007.125</langsyntaxhighlight>
 
=={{header|Run BASIC}}==
<lang runbasic>print right$("00000";using("#####.##",7.125),8) ' => 00007.13</lang>
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">
fn main() {
let x = 7.125;
Line 2,015 ⟶ 2,364:
println!("{:09}", -x);
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,027 ⟶ 2,376:
The Fill options should fill with any character, but it is still (!) not implemented; according to [http://www.icsi.berkeley.edu/~sather/Documentation/Library/LibraryBrowser/short-FMT.html ICSI Sather library documentation] (GNU Sather library documentation is missing) works only for string, bools and characters, but a test has revealed it does not work in either way (yet) (GNU Sather v1.2.3).
 
<langsyntaxhighlight lang="sather">class MAIN is
main is
#OUT + #FMT("<F0 #####.###>", 7.1257) + "\n";
#OUT + #FMT("<F0 #####.###>", 7.1254) + "\n";
end;
end;</langsyntaxhighlight>
 
Luckly the C-like formats are supported too:
 
<langsyntaxhighlight lang="sather"> #OUT + #FMT("%09.3f", 7.125) + "\n";</langsyntaxhighlight>
 
=={{header|Scala}}==
Line 2,042 ⟶ 2,391:
{{works with|Scala|2.10.2}}
As shown in a [https://github.com/scala-ide/scala-worksheet/wiki/Getting-Started Scala Worksheet]:
<langsyntaxhighlight Scalalang="scala">object FormattedNumeric {
val r = 7.125 //> r : Double = 7.125
println(f" ${-r}%9.3f"); //> -7,125
Line 2,051 ⟶ 2,400:
println(f" $r%-9.3f"); //> 7,125
println(f" $r%+09.3f"); //> +0007,125
}</langsyntaxhighlight>
 
=={{header|Scheme}}==
Line 2,060 ⟶ 2,409:
Gauche/share/gauche/site/lib/
 
<langsyntaxhighlight Schemelang="scheme">(load "srfi-54.scm")
(load "srfi-54.scm") ;; Don't ask.
 
Line 2,067 ⟶ 2,416:
(dotimes (i 4)
(print (cat x 25 3.0 #\0 (list #\, (- 4 i)))))
</syntaxhighlight>
</lang>
{{output}}
<pre>
Line 2,077 ⟶ 2,426:
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "float.s7i";
 
Line 2,090 ⟶ 2,439:
writeln( r digits 3);
writeln(-r digits 3);
end func;</langsyntaxhighlight>
 
{{out}}
Line 2,103 ⟶ 2,452:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">printf("%09.3f\n", 7.125);</langsyntaxhighlight>
or
<langsyntaxhighlight lang="ruby">say ("%09.3f" % 7.125);</langsyntaxhighlight>
{{out}}
<pre>
Line 2,113 ⟶ 2,462:
=={{header|Smalltalk}}==
{{works with|Pharo 1.1.1}}
<langsyntaxhighlight lang="smalltalk">Transcript show: (7.125 printPaddedWith: $0 to: 3.6); cr.
"output: 007.125000"</langsyntaxhighlight>
 
{{works with|Smalltalk/X}}
<langsyntaxhighlight lang="smalltalk">(7.123 asFixedPoint:3) printOn: Transcript leftPaddedTo: 9 with: $0
"output: 00007.125"</langsyntaxhighlight>
notice that printOn:* is implemented in Object;thus any object can be printed with padding this way.
 
Using the PrintfScanf utility:
<langsyntaxhighlight lang="smalltalk">PrintfScanf new printf:'%08.3f' arguments: { 7.125 }</langsyntaxhighlight>
 
=={{header|SQL}}==
{{works with|MS SQL|2005}}
<langsyntaxhighlight lang="sql">declare @n int
select @n=123
select substring(convert(char(5), 10000+@n),2,4) as FourDigits
Line 2,132 ⟶ 2,481:
set @n=5
print "TwoDigits: " + substring(convert(char(3), 100+@n),2,2)
--Output: 05</langsyntaxhighlight>
 
=={{header|Standard ML}}==
<langsyntaxhighlight lang="sml">print (StringCvt.padLeft #"0" 9 (Real.fmt (StringCvt.FIX (SOME 3)) 7.125) ^ "\n")</langsyntaxhighlight>
{{works with|SML/NJ}}
<langsyntaxhighlight lang="sml">print (Format.format "%09.3f\n" [Format.REAL 7.125])</langsyntaxhighlight>
 
=={{header|Stata}}==
See '''[https://www.stata.com/help.cgi?format format]''' in Stata help.
 
<langsyntaxhighlight lang="stata">. display %010.3f (57/8)
000007.125</langsyntaxhighlight>
 
=={{header|Suneido}}==
<syntaxhighlight lang Suneido="suneido">Print(7.125.Pad(9))</langsyntaxhighlight>
 
{{out}}
Line 2,152 ⟶ 2,501:
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl">set number 7.342
format "%08.3f" $number</langsyntaxhighlight>
Use with <tt>puts</tt> if output is desired to go to a channel.
 
=={{header|TI-89 BASIC}}==
 
{{improve|TI-89 BASIC|It does not handle negative numbers.}}
 
<lang ti89b>right("00000" & format(7.12511, "f3"), 9)</lang>
 
=={{header|Toka}}==
<langsyntaxhighlight lang="toka">needs values
value n
123 to n
 
2 import printf
" %08d" n printf</langsyntaxhighlight>
 
=={{header|Ursala}}==
Line 2,174 ⟶ 2,517:
library function by that name and can cope with any of
the same numeric formats.
<langsyntaxhighlight Ursalalang="ursala">#import flo
 
x = 7.125
Line 2,180 ⟶ 2,523:
#show+
 
t = <printf/'%09.3f' x></langsyntaxhighlight>
{{out}}
<pre>00007.125</pre>
 
=={{header|Vala}}==
<langsyntaxhighlight Valalang="vala">void main() {
double r = 7.125;
print(" %9.3f\n", -r);
Line 2,193 ⟶ 2,536:
print(" %09.3f\n",r);
print(" %-09.3f\n",r);
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,203 ⟶ 2,546:
7.125
</pre>
 
=={{header|VBA}}==
<lang vb>Option Explicit
 
Sub Main()
Debug.Print fFormat(13, 2, 1230.3333)
Debug.Print fFormat(2, 13, 1230.3333)
Debug.Print fFormat(10, 5, 0.3333)
Debug.Print fFormat(13, 2, 1230)
End Sub
 
Private Function fFormat(NbInt As Integer, NbDec As Integer, Nb As Double) As String
'NbInt : Lenght of integral part
'NbDec : Lenght of decimal part
'Nb : decimal on integer number
Dim u As String, v As String, i As Integer
u = CStr(Nb)
i = InStr(u, Application.DecimalSeparator)
If i > 0 Then
v = Mid(u, i + 1)
u = Left(u, i - 1)
fFormat = Right(String(NbInt, "0") & u, NbInt) & Application.DecimalSeparator & Left(v & String(NbDec, "0"), NbDec)
Else
fFormat = Right(String(NbInt, "0") & u, NbInt) & Application.DecimalSeparator & String(NbDec, "0")
End If
End Function
</lang>
{{out}}
<pre>0000000001230.33
30.3333000000000
0000000000.33330
0000000001230.00</pre>
 
=={{header|VBScript}}==
{{works with|Windows Script Host|*}}
<lang VBScript>
a = 1234.5678
 
' Round to three decimal places. Groups by default. Output = "1,234.568".
WScript.Echo FormatNumber(a, 3)
 
' Truncate to three decimal places. Output = "1234.567".
WScript.Echo Left(a, InStr(a, ".") + 3)
 
' Round to a whole number. Grouping disabled. Output = "1235".
WScript.Echo FormatNumber(a, 0, , , False)
 
' Use integer portion only and pad with zeroes to fill 8 chars. Output = "00001234".
WScript.Echo Right("00000000" & Int(a), 8)
</lang>
 
=={{header|Vedit macro language}}==
Line 2,258 ⟶ 2,551:
The following example uses 3 decimal places (value scaled by 1000).
The output is inserted at current edit position.
<langsyntaxhighlight lang="vedit">#1 = 7125
Num_Ins(#1, FILL+COUNT, 9) Char(-3) Ins_Char('.')</langsyntaxhighlight>
 
{{out}}
<pre>
Line 2,266 ⟶ 2,558:
</pre>
 
=={{header|VisualV Basic(Vlang)}}==
<syntaxhighlight lang="Zig">
{{works with|Visual Basic|VB6 Standard}}
fn main() {
<lang vb>
// refer to string interpolation and format place holding in documentation
Debug.Print Format$(7.125, "00000.000")
// pad with zeros towards the left
</lang>
num := 7.125
Output (the decimal separator used depends on the system's language settings):
println("${num:09f}")
<lang vb>
}
</syntaxhighlight>
 
{{out}}
<pre>
00007.125
</langpre>
 
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./fmt" for Fmt
 
var n = 7.125
System.print(Fmt.rjust(9, n, "0"))</langsyntaxhighlight>
 
{{out}}
Line 2,289 ⟶ 2,586:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">int C;
[Format(5, 3); \5 places before decimal point and 3 after
RlOut(8, 7.125); \output real number to internal buffer
Line 2,297 ⟶ 2,594:
ChOut(0, C); \display digit character on terminal
];
]</langsyntaxhighlight>
 
{{out}}
Line 2,308 ⟶ 2,605:
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">"%09.3f".fmt(7.125) //-->"00007.125"
"%09.3e".fmt(7.125) //-->"7.125e+00"
"%09.3g".fmt(7.125) //-->"000007.12"
"%09d".fmt(7.125) //-->"000000007"
"%09,d".fmt(78901.125)//-->"00078,901"</langsyntaxhighlight>
 
=={{header|ZX Spectrum Basic}}==
<lang zxbasic>10 LET n=7.125
20 LET width=9
30 GO SUB 1000
40 PRINT AT 10,10;n$
50 STOP
1000 REM Formatted fixed-length
1010 LET n$=STR$ n
1020 FOR i=1 TO width-LEN n$
1030 LET n$="0"+n$
1040 NEXT i
1050 RETURN </lang>
511

edits