Formatted numeric output: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
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 219:
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:
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:
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
<lang ARM Assembly>
/* ARM assembly Raspberry PI */
/* program formatNum.s */
Line 295:
 
 
</syntaxhighlight>
</lang>
 
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">r: 7.125
 
print r
print to :string .format: "09.3f" r</langsyntaxhighlight>
 
{{out}}
Line 311:
=={{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:
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:
printf " %09.3f\n",r
printf " %-09.3f\n",r
}</langsyntaxhighlight>
 
Same output as the C code.
Line 336:
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 345:
 
=={{header|BBC BASIC}}==
<langsyntaxhighlight lang="bbcbasic"> PRINT FNformat(PI, 9, 3)
PRINT FNformat(-PI, 9, 3)
END
Line 355:
= RIGHT$(STRING$(sl%,"0") + STR$(n), sl%)
ENDIF
= "-" + RIGHT$(STRING$(sl%,"0") + STR$(-n), sl%-1)</langsyntaxhighlight>
{{out}}
<pre>
Line 365:
First define a custom function for numeric output.
 
<langsyntaxhighlight lang="bc">/*
* Print number n, using at least c characters.
*
Line 441:
 
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:
"Binary: "; t = p(x, 1001); "
"
quit</langsyntaxhighlight>
 
{{out}}
Line 462:
 
=={{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:
printf(" %-09.3f\n",r);
return 0;
}</langsyntaxhighlight>
{{out}}
-7.125
Line 489:
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">
class Program
{
Line 510:
}
</syntaxhighlight>
</lang>
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <iomanip>
 
Line 520:
std::cout << std::setfill('0') << std::setw(9) << std::fixed << std::setprecision(3) << 7.125 << std::endl;
return 0;
}</langsyntaxhighlight>
C++20
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <format>
Line 529:
std::cout << std::format("{:09.3f}\n", 7.125);
return 0;
}</langsyntaxhighlight>
 
=={{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 550:
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 568:
writefln(" %09.3f", r);
writefln(" %-09.3f", r);
}</langsyntaxhighlight>
{{out}}
<pre> -7.125
Line 578:
 
=={{header|DBL}}==
<langsyntaxhighlight DBLlang="dbl">D5=7125
A10=D5,'-ZZZZX.XXX' ; 7.125
A10=D5,'-ZZZZX.XXX' [LEFT] ;7.125
Line 589:
A10=-D5,'ZZZZX.XXX-' [LEFT] ;7.125-
 
A10=1500055,'ZZZ,ZZX.XX ; 15,000.55</langsyntaxhighlight>
 
=={{header|dc}}==
Line 596:
First define a custom function for numeric output.
 
<langsyntaxhighlight lang="dc">[*
* (n) (c) lpx
* Print number n, using at least c characters.
Line 707:
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 724:
=={{header|Delphi}}==
 
<syntaxhighlight lang="delphi">
<lang Delphi>
program FormattedNumericOutput;
 
Line 744:
Readln;
end.
</syntaxhighlight>
</lang>
 
{{out}}
Line 759:
{{works with|Eiffel Studio|6.6}}
 
<syntaxhighlight lang="eiffel">
<lang Eiffel>
note
description : "{
Line 827:
 
end
</syntaxhighlight>
</lang>
 
=={{header|Elixir}}==
 
<langsyntaxhighlight lang="elixir">n = 7.125
:io.fwrite "~f~n", [n]
:io.fwrite "~.3f~n", [n]
Line 844:
:io.fwrite "~e~n", [n]
:io.fwrite "~12.4e~n", [n]
:io.fwrite "~12.4.0e~n", [n]</langsyntaxhighlight>
 
{{out}}
Line 864:
 
=={{header|Emacs Lisp}}==
<langsyntaxhighlight Lisplang="lisp">(format "%09.3f" 7.125) ;=> "00007.125"</langsyntaxhighlight>
 
<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 877:
 
=={{header|ERRE}}==
<langsyntaxhighlight ERRElang="erre">PROGRAM FORMATTED
 
PROCEDURE FORMATTED_PRINT(N,LENGTH,DEC_PLACES->FP$)
Line 917:
END FOR
 
END PROGRAM</langsyntaxhighlight>
{{out}}
<pre>
Line 933:
 
=={{header|Euphoria}}==
<langsyntaxhighlight lang="euphoria">constant r = 7.125
printf(1,"%9.3f\n",-r)
printf(1,"%9.3f\n",r)
Line 939:
printf(1,"%09.3f\n",-r)
printf(1,"%09.3f\n",r)
printf(1,"%-09.3f\n",r)</langsyntaxhighlight>
 
{{out}}
Line 952:
 
=={{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 964:
=={{header|Fantom}}==
 
<langsyntaxhighlight lang="fantom">
class Main
{
Line 972:
}
}
</syntaxhighlight>
</lang>
 
=={{header|Forth}}==
Line 983:
(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 990:
 
\ 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,015:
{{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,027:
1 FORMAT (F9.3,T1,I5.5)
END
</syntaxhighlight>
</lang>
Output is
00007.125
Line 1,038:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
#Include "vbcompat.bi"
Line 1,044:
Dim s As String = Format(7.125, "00000.0##")
Print s
Sleep</langsyntaxhighlight>
 
{{out}}
Line 1,052:
 
=={{header|FutureBasic}}==
<langsyntaxhighlight lang="futurebasic">window 1, @"Formatted Numeric Output", (0,0,480,270)
 
print using "0000#.###";7.125
 
HandleEvents</langsyntaxhighlight>
Output:
<pre>
Line 1,072:
=={{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,083:
 
=={{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,096:
 
=={{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,109:
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,137:
print(FixedLengthFormat(-d, 9))
}
</syntaxhighlight>
</lang>
 
=={{header|Icon}} and {{header|Unicon}}==
<langsyntaxhighlight Iconlang="icon">link printf
 
procedure main()
Line 1,149:
write(sprintf(p,r)," <=== sprintf ",p)
}
end</langsyntaxhighlight>
 
{{out}} Abbreviated
Line 1,165:
[[Category:IDL]]
 
<langsyntaxhighlight lang="idl">n = 7.125
print, n, format='(f08.3)'
;==> 0007.125</langsyntaxhighlight>
 
=={{header|IS-BASIC}}==
<langsyntaxhighlight ISlang="is-BASICbasic">100 LET F=7.125
110 PRINT USING "-%%%%%.###":F</langsyntaxhighlight>
 
=={{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,182:
{{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,188:
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,213:
System.out.println(numForm.format(7.135));//rounds to even
}
}</langsyntaxhighlight>
{{out}}
<pre>000000007.125
Line 1,221:
 
=={{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,233:
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,247:
| 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,280:
printfmtln(fe, i)
end
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,310:
 
=={{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,324:
=={{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,355:
>>>>>>>> -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>
Line 1,366:
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">
<lang lb>
for i =1 to 10
n =rnd( 1) *10^( int( 10 *rnd(1)) -2)
Line 1,387:
FormattedPrint$ =right$( "000000000000" +nn$, length) ' chop to required length
end function
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,405:
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,423:
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,454:
+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|Mercury}}==
<syntaxhighlight lang="text">
:- module formatted_numeric_output.
:- interface.
Line 1,477:
main(!IO) :-
io.format("%09.3f\n", [f(7.125)], !IO).
</syntaxhighlight>
</lang>
 
=={{header|min}}==
{{works with|min|0.19.6}}
<langsyntaxhighlight 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>
Line 1,492:
=={{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,517:
 
return
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,526:
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">import strformat
const r = 7.125
echo r
Line 1,532:
echo fmt"{r:9.3f}"
echo fmt"{-r:09.3f}"
echo fmt"{r:09.3f}"</langsyntaxhighlight>
{{out}}
<pre>7.125
Line 1,543:
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,564:
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,574:
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,616:
// 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,633:
 
=={{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,673:
Put Skip List(z,s);
End;
End;</langsyntaxhighlight>
{{out}}
<pre>
Line 1,686:
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,704:
;;; 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.
<langsyntaxhighlight PureBasiclang="purebasic">RSet(StrF(7.125,3),8,"0") ; Will be 0007.125</langsyntaxhighlight>
 
=={{header|Python}}==
Line 1,722:
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,731:
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,745:
 
{{works with|Python|3}}
<langsyntaxhighlight lang="python">from math import pi, exp
r = exp(pi)-pi
print(r)
Line 1,754:
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,771:
[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,798:
[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,809:
=={{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,835:
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,844:
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,882:
print [zeropad 9 negate 7.125]
print [zeropad 9 7.125]
print 7.125</langsyntaxhighlight>
 
{{out}}
Line 1,893:
 
=={{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,952:
say 't=' t
say 'u=' u
/*stick a fork in it, we're done.*/</langsyntaxhighlight>
{{out}}
<pre style="height:30ex">
Line 1,987:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
decimals(3)
see fixedprint(7.125, 5) + nl
Line 1,996:
next
see num + nl
</syntaxhighlight>
</lang>
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">r = 7.125
printf " %9.3f\n", r #=> 7.125
printf " %09.3f\n", r #=> 00007.125
Line 2,007:
puts " %09.3f" % r #=> 00007.125
puts " %09.3f" % -r #=> -0007.125
puts " %+09.3f" % r #=> +0007.125</langsyntaxhighlight>
 
=={{header|Run BASIC}}==
<langsyntaxhighlight lang="runbasic">print right$("00000";using("#####.##",7.125),8) ' => 00007.13</langsyntaxhighlight>
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">
fn main() {
let x = 7.125;
Line 2,022:
println!("{:09}", -x);
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,034:
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,049:
{{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,058:
println(f" $r%-9.3f"); //> 7,125
println(f" $r%+09.3f"); //> +0007,125
}</langsyntaxhighlight>
 
=={{header|Scheme}}==
Line 2,067:
Gauche/share/gauche/site/lib/
 
<langsyntaxhighlight Schemelang="scheme">(load "srfi-54.scm")
(load "srfi-54.scm") ;; Don't ask.
 
Line 2,074:
(dotimes (i 4)
(print (cat x 25 3.0 #\0 (list #\, (- 4 i)))))
</syntaxhighlight>
</lang>
{{output}}
<pre>
Line 2,084:
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "float.s7i";
 
Line 2,097:
writeln( r digits 3);
writeln(-r digits 3);
end func;</langsyntaxhighlight>
 
{{out}}
Line 2,110:
 
=={{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,120:
=={{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,139:
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,159:
 
=={{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.
 
Line 2,167:
{{improve|TI-89 BASIC|It does not handle negative numbers.}}
 
<langsyntaxhighlight lang="ti89b">right("00000" & format(7.12511, "f3"), 9)</langsyntaxhighlight>
 
=={{header|Toka}}==
<langsyntaxhighlight lang="toka">needs values
value n
123 to n
 
2 import printf
" %08d" n printf</langsyntaxhighlight>
 
=={{header|Ursala}}==
Line 2,181:
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,187:
#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,200:
print(" %09.3f\n",r);
print(" %-09.3f\n",r);
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,212:
 
=={{header|VBA}}==
<langsyntaxhighlight lang="vb">Option Explicit
 
Sub Main()
Line 2,236:
End If
End Function
</syntaxhighlight>
</lang>
{{out}}
<pre>0000000001230.33
Line 2,245:
=={{header|VBScript}}==
{{works with|Windows Script Host|*}}
<syntaxhighlight lang="vbscript">
<lang VBScript>
a = 1234.5678
 
Line 2,259:
' Use integer portion only and pad with zeroes to fill 8 chars. Output = "00001234".
WScript.Echo Right("00000000" & Int(a), 8)
</syntaxhighlight>
</lang>
 
=={{header|Vedit macro language}}==
Line 2,265:
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}}
Line 2,275:
=={{header|Visual Basic}}==
{{works with|Visual Basic|VB6 Standard}}
<syntaxhighlight lang="vb">
<lang vb>
Debug.Print Format$(7.125, "00000.000")
</syntaxhighlight>
</lang>
Output (the decimal separator used depends on the system's language settings):
<syntaxhighlight lang="vb">
<lang vb>
00007.125
</syntaxhighlight>
</lang>
 
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "/fmt" for Fmt
 
var n = 7.125
System.print(Fmt.rjust(9, n, "0"))</langsyntaxhighlight>
 
{{out}}
Line 2,296:
 
=={{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,304:
ChOut(0, C); \display digit character on terminal
];
]</langsyntaxhighlight>
 
{{out}}
Line 2,315:
 
=={{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}}==
<langsyntaxhighlight lang="zxbasic">10 LET n=7.125
20 LET width=9
30 GO SUB 1000
Line 2,332:
1030 LET n$="0"+n$
1040 NEXT i
1050 RETURN </langsyntaxhighlight>
10,333

edits