Formatted numeric output: Difference between revisions
m
syntax highlighting fixup automation
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
|||
Line 12:
=={{header|11l}}==
<
{{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.
<
.stack 1024
.data
Line 68:
LeadingZeroes byte 4 ;number of leading zeroes to print
TrailingZeroes byte 0 ;number of trailing zeroes to print</
{{out}}
Line 78:
=={{header|8th}}==
<
7.125 "%09.3f" s:strfmt
. cr
</syntaxhighlight>
{{out}}
<pre>
Line 89:
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang="aarch64 assembly">
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program formatNum64.s */
Line 137:
qAdrsfNumber1: .quad sfNumber1
</syntaxhighlight>
{{Output}}
<pre>
Line 145:
=={{header|Ada}}==
<
with Ada.Text_Io; use Ada.Text_Io;
Line 158:
begin
Put(Item => Value, Pic => Pic);
end Zero_Fill;</
{{out}}
<pre>
Line 165:
=={{header|Aime}}==
<
o_form("/w12d6p6/\n", -12.0625);
o_form("/w12d6p6/\n", 7.125);</
{{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}}
<
REAL r=exp(pi)-pi;
print((r,newline));
Line 196:
printf(($zzzz-d.ddddeddl$,r));
printf(($4z-d.4de4dl$,r))
)</
{{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)
<
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</
=={{header|APL}}==
<
00007.125000000</
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">
/* ARM assembly Raspberry PI */
/* program formatNum.s */
Line 295:
</syntaxhighlight>
=={{header|Arturo}}==
<
print r
print to :string .format: "09.3f" r</
{{out}}
Line 311:
=={{header|AutoHotkey}}==
contributed by Laszlo on the ahk [http://www.autohotkey.com/forum/post-276467.html#276467 forum]
<
MsgBox % pad(-7.25,7) ; -007.25
Line 318:
VarSetCapacity(p,len,Asc("0"))
Return SubStr(p x,1-len)
}</
=={{header|AWK}}==
<
r=7.125
printf " %9.3f\n",-r
Line 329:
printf " %09.3f\n",r
printf " %-09.3f\n",r
}</
Same output as the C code.
Line 336:
BaCon can use C style <tt>printf</tt> format specifiers.
<
n = 7.125
PRINT n FORMAT "%09.3f\n"</
{{out}}
Line 345:
=={{header|BBC BASIC}}==
<
PRINT FNformat(-PI, 9, 3)
END
Line 355:
= RIGHT$(STRING$(sl%,"0") + STR$(n), sl%)
ENDIF
= "-" + RIGHT$(STRING$(sl%,"0") + STR$(-n), sl%-1)</
{{out}}
<pre>
Line 365:
First define a custom function for numeric output.
<
* Print number n, using at least c characters.
*
Line 441:
scale = s /* Restore original scale. */
}</
Then use this function to print 7.125 with 9 characters.
<
"Decimal: "; t = p(x, 9); "
"
Line 454:
"Binary: "; t = p(x, 1001); "
"
quit</
{{out}}
Line 462:
=={{header|Beads}}==
<
calc main_init
var num = 7.125
log to_str(num, min:9, zero_pad:Y)</
{{out}}
<pre>00007.125</pre>
=={{header|C}}==
<
main(){
float r=7.125;
Line 479:
printf(" %-09.3f\n",r);
return 0;
}</
{{out}}
-7.125
Line 489:
=={{header|C sharp|C#}}==
<
class Program
{
Line 510:
}
</syntaxhighlight>
=={{header|C++}}==
<
#include <iomanip>
Line 520:
std::cout << std::setfill('0') << std::setw(9) << std::fixed << std::setprecision(3) << 7.125 << std::endl;
return 0;
}</
C++20
<
#include <format>
Line 529:
std::cout << std::format("{:09.3f}\n", 7.125);
return 0;
}</
=={{header|Clojure}}==
{{trans|Common Lisp}} Using cl format strings
<
{{trans|java}} Using java format strings
<
=={{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).
<
PROGRAM-ID. NUMERIC-OUTPUT-PROGRAM.
DATA DIVISION.
Line 550:
MOVE 7.125 TO X.
DISPLAY X UPON CONSOLE.
STOP RUN.</
{{out}}
<pre>00007.125</pre>
=={{header|Common Lisp}}==
<
=={{header|D}}==
<
void main() {
Line 568:
writefln(" %09.3f", r);
writefln(" %-09.3f", r);
}</
{{out}}
<pre> -7.125
Line 578:
=={{header|DBL}}==
<
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</
=={{header|dc}}==
Line 596:
First define a custom function for numeric output.
<
* (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</
Then use this function to print 7.125 with 9 characters:
<
[Decimal: ]P lx 9 lpx [
]P 16 i [Hexadecimal: ]P lx 9 lpx [
]P 2 i [Binary: ]P lx 9 lpx [
]P</
{{out}}
Line 724:
=={{header|Delphi}}==
<syntaxhighlight lang="delphi">
program FormattedNumericOutput;
Line 744:
Readln;
end.
</syntaxhighlight>
{{out}}
Line 759:
{{works with|Eiffel Studio|6.6}}
<syntaxhighlight lang="eiffel">
note
description : "{
Line 827:
end
</syntaxhighlight>
=={{header|Elixir}}==
<
: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]</
{{out}}
Line 864:
=={{header|Emacs Lisp}}==
<
<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}}==
<
PROCEDURE FORMATTED_PRINT(N,LENGTH,DEC_PLACES->FP$)
Line 917:
END FOR
END PROGRAM</
{{out}}
<pre>
Line 933:
=={{header|Euphoria}}==
<
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)</
{{out}}
Line 952:
=={{header|F_Sharp|F#}}==
<
=={{header|Factor}}==
<
7.125 "%09.3f\n" printf</
{{out}}
<pre>
Line 964:
=={{header|Fantom}}==
<
class Main
{
Line 972:
}
}
</syntaxhighlight>
=={{header|Forth}}==
Line 983:
(The 1- is necessary because #s always generates at least one digit, even if it's zero.)
<
: #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 ;</
Usage example:
<
Result: 00000123 ok
Type: -123 s>d 8 d.0
Result: -00000123 ok</
===={{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.
<
WRITE(*,"(I8.8)") number ! Prints 00007125</
===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">
INTEGER IV
REAL V
Line 1,027:
1 FORMAT (F9.3,T1,I5.5)
END
</syntaxhighlight>
Output is
00007.125
Line 1,038:
=={{header|FreeBASIC}}==
<
#Include "vbcompat.bi"
Line 1,044:
Dim s As String = Format(7.125, "00000.0##")
Print s
Sleep</
{{out}}
Line 1,052:
=={{header|FutureBasic}}==
<
print using "0000#.###";7.125
HandleEvents</
Output:
<pre>
Line 1,072:
=={{header|Gambas}}==
'''[https://gambas-playground.proko.eu/?gist=4f64ab96d0f97df5876b76ab0431b302 Click this link to run this code]'''
<
Print Format("7.125", "00000.000")
End</
Output:
<pre>
Line 1,083:
=={{header|gnuplot}}==
<
=={{header|Go}}==
<
=={{header|Groovy}}==
Solution:
<
{{out}}
Line 1,096:
=={{header|Haskell}}==
<
main =
printf "%09.3f" 7.125</
=={{header|hexiscript}}==
<
let n tostr n
while len n < length
Line 1,109:
endfun
format 7.125 9</
=={{header|HicEst}}==
<
=={{header|i}}==
<syntaxhighlight lang="i">
concept FixedLengthFormat(value, length) {
string = text(abs(value))
Line 1,137:
print(FixedLengthFormat(-d, 9))
}
</syntaxhighlight>
=={{header|Icon}} and {{header|Unicon}}==
<
procedure main()
Line 1,149:
write(sprintf(p,r)," <=== sprintf ",p)
}
end</
{{out}} Abbreviated
Line 1,165:
[[Category:IDL]]
<
print, n, format='(f08.3)'
;==> 0007.125</
=={{header|IS-BASIC}}==
<
110 PRINT USING "-%%%%%.###":F</
=={{header|J}}==
<
00007.125</
[http://www.jsoftware.com/help/dictionary/dx008.htm Documentation on 8!:]
Line 1,182:
{{works with|Java|1.5+}}
Stealing printf from C/C++:
<
public static void main(String[] args){
double value = 7.125;
Line 1,188:
System.out.println(String.format("%09.3f",value));
}
}</
{{out}}
<pre>000000007.125
000000007.125</pre>
Using <code>NumberFormat</code>:
<
import java.text.NumberFormat;
Line 1,213:
System.out.println(numForm.format(7.135));//rounds to even
}
}</
{{out}}
<pre>000000007.125
Line 1,221:
=={{header|JavaScript}}==
<
var str = ("00000" + n).slice(-5);
alert(str);</
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.
<
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);</
'''Examples''':
<
produces
<
00000012.3
000333.333
0001000000</
<
produces
<
0012.3
0333.33
1000000.</
=={{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").
<
test = [7.125, [rand()*10^rand(0:4) for i in 1:9]]
Line 1,280:
printfmtln(fe, i)
end
</syntaxhighlight>
{{out}}
Line 1,310:
=={{header|Kotlin}}==
<
fun main(args: Array<String>) {
val num = 7.125
println("%09.3f".format(num))
}</
{{out}}
Line 1,324:
=={{header|Lambdatalk}}==
Lambdatalk has no primitive for numeric output. This is a way to define it:
<
{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>
=={{header|Lasso}}==
<
{{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">
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>
{{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.
<
output map [ifelse ? = "| | ["0] [?]] form :num :width :precision
end
print zpad 7.125 9 3 ; 00007.125</
{{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.
<
=={{header|Lua}}==
<
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</
An easier way to do that would be
<syntaxhighlight lang="lua">
print(string.format("%09.3d",7.125))
</syntaxhighlight>
=={{header|M2000 Interpreter}}==
We can use ? as Print
<syntaxhighlight lang="m2000 interpreter">
Print str$(7.125,"00000.000")
</syntaxhighlight>
=={{header|Maple}}==
<
3.141593
printf("%.0f", Pi);
Line 1,454:
+0003.14
printf("%+0*.*f",8, 2, Pi);
+0003.14</
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<
00007.125</
=={{header|MATLAB}} / {{header|Octave}}==
<
00007.125</
=={{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>
=={{header|min}}==
{{works with|min|0.19.6}}
<
(pick length - repeat swap str-append) :left-pad
7.125 string "0" 9 left-pad puts!</
{{out}}
<pre>
Line 1,492:
=={{header|Modula-3}}==
Modules <tt>IO</tt> and <tt>Fmt</tt> must be imported before use.
<
=={{header|Nanoquery}}==
<
println format("%09.3f", printer)</
{{out}}
<pre>00007.125</pre>
=={{header|NetRexx}}==
<
options replace format comments java crossref savelog symbols binary
Line 1,517:
return
</syntaxhighlight>
{{out}}
<pre>
Line 1,526:
=={{header|Nim}}==
<
const r = 7.125
echo r
Line 1,532:
echo fmt"{r:9.3f}"
echo fmt"{-r:09.3f}"
echo fmt"{r:09.3f}"</
{{out}}
<pre>7.125
Line 1,543:
Module <code>Out</code> must be imported before use.
<
=={{header|Objective-C}}==
<
or
<
=={{header|OCaml}}==
<
=={{header|OpenEdge/Progress}}==
<
STRING( 7.125, "99999.999" )
VIEW-AS ALERT-BOX.</
=={{header|Oz}}==
Line 1,564:
It doesn't seem to be possible to use leading zeros for printing,
so we implement this manually:
<
fun {PrintFloat X Prec}
{Property.put 'print.floatPrecision' Prec}
Line 1,574:
end
in
{System.showInfo {PrintFloat 7.125 8}}</
=={{header|PARI/GP}}==
{{works with|PARI/GP|2.4.3 and above}}
<
=={{header|Pascal}}==
<
const
wholeNumberPlaces = 5;
Line 1,616:
// rounded
write(n:0:fractionalPlaces);
end;</
=={{header|Perl}}==
{{works with|Perl|5.x}}
<
=={{header|Phix}}==
{{libheader|Phix/basics}}
<!--<
<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;">)
<!--</
{{out}}
<pre>
Line 1,633:
=={{header|PHP}}==
<
or
<
=={{header|PicoLisp}}==
<
(pad 9 (format 7125 3 ",")) # European format</
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">
put edit (X) (p'999999.V999'); /* Western format. */
put edit (X) (p'999999,V999'); /* In European format. */
</syntaxhighlight>
<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;</
{{out}}
<pre>
Line 1,686:
The task is underspecified, so we present a few alternatives.
<
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</
=={{header|PowerShell}}==
Using the <code>-f</code> formatting operator and a custom format string:
<
or by invoking <code>ToString</code> on the number:
<
=={{header|PureBasic}}==
Using RSet() to pad 7.125 with 3 decimals converted to a string, to 8 char length.
<
=={{header|Python}}==
Line 1,722:
specifying how many digits appear in the exponent when printed with a format.
<
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)</
{{out}}
<pre>
Line 1,745:
{{works with|Python|3}}
<
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))</
{{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:
<
[1] "3.141593"
> sprintf("%.3f", pi)
Line 1,798:
[1] "3141592.65"
> sprintf("%G", 1e-6 * pi)
[1] "3.14159E-06"</
formatC also provides C-style string formatting.
<
# "00007.125"</
Other string formatting functions include
Line 1,809:
=={{header|Racket}}==
<syntaxhighlight lang="racket">
-> (displayln (~a 7.125 #:width 9 #:align 'right #:pad-string "0"))
00007.125
</syntaxhighlight>
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku"
=={{header|Raven}}==
<
00007.125</
{{trans|Python}}
<
-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</
<
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 !</
=={{header|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</
{{out}}
Line 1,893:
=={{header|REXX}}==
<
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.*/</
{{out}}
<pre style="height:30ex">
Line 1,987:
=={{header|Ring}}==
<
decimals(3)
see fixedprint(7.125, 5) + nl
Line 1,996:
next
see num + nl
</syntaxhighlight>
=={{header|Ruby}}==
<
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</
=={{header|Run BASIC}}==
<
=={{header|Rust}}==
<
fn main() {
let x = 7.125;
Line 2,022:
println!("{:09}", -x);
}
</syntaxhighlight>
{{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).
<
main is
#OUT + #FMT("<F0 #####.###>", 7.1257) + "\n";
#OUT + #FMT("<F0 #####.###>", 7.1254) + "\n";
end;
end;</
Luckly the C-like formats are supported too:
<
=={{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]:
<
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
}</
=={{header|Scheme}}==
Line 2,067:
Gauche/share/gauche/site/lib/
<
(load "srfi-54.scm") ;; Don't ask.
Line 2,074:
(dotimes (i 4)
(print (cat x 25 3.0 #\0 (list #\, (- 4 i)))))
</syntaxhighlight>
{{output}}
<pre>
Line 2,084:
=={{header|Seed7}}==
<
include "float.s7i";
Line 2,097:
writeln( r digits 3);
writeln(-r digits 3);
end func;</
{{out}}
Line 2,110:
=={{header|Sidef}}==
<
or
<
{{out}}
<pre>
Line 2,120:
=={{header|Smalltalk}}==
{{works with|Pharo 1.1.1}}
<
"output: 007.125000"</
{{works with|Smalltalk/X}}
<
"output: 00007.125"</
notice that printOn:* is implemented in Object;thus any object can be printed with padding this way.
Using the PrintfScanf utility:
<
=={{header|SQL}}==
{{works with|MS SQL|2005}}
<
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</
=={{header|Standard ML}}==
<
{{works with|SML/NJ}}
<
=={{header|Stata}}==
See '''[https://www.stata.com/help.cgi?format format]''' in Stata help.
<
000007.125</
=={{header|Suneido}}==
<syntaxhighlight lang
{{out}}
Line 2,159:
=={{header|Tcl}}==
<
format "%08.3f" $number</
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.}}
<
=={{header|Toka}}==
<
value n
123 to n
2 import printf
" %08d" n printf</
=={{header|Ursala}}==
Line 2,181:
library function by that name and can cope with any of
the same numeric formats.
<
x = 7.125
Line 2,187:
#show+
t = <printf/'%09.3f' x></
{{out}}
<pre>00007.125</pre>
=={{header|Vala}}==
<
double r = 7.125;
print(" %9.3f\n", -r);
Line 2,200:
print(" %09.3f\n",r);
print(" %-09.3f\n",r);
}</
{{out}}
<pre>
Line 2,212:
=={{header|VBA}}==
<
Sub Main()
Line 2,236:
End If
End Function
</syntaxhighlight>
{{out}}
<pre>0000000001230.33
Line 2,245:
=={{header|VBScript}}==
{{works with|Windows Script Host|*}}
<syntaxhighlight 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>
=={{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.
<
Num_Ins(#1, FILL+COUNT, 9) Char(-3) Ins_Char('.')</
{{out}}
Line 2,275:
=={{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|Wren}}==
{{libheader|Wren-fmt}}
<
var n = 7.125
System.print(Fmt.rjust(9, n, "0"))</
{{out}}
Line 2,296:
=={{header|XPL0}}==
<
[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
];
]</
{{out}}
Line 2,315:
=={{header|zkl}}==
<
"%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"</
=={{header|ZX Spectrum Basic}}==
<
20 LET width=9
30 GO SUB 1000
Line 2,332:
1030 LET n$="0"+n$
1040 NEXT i
1050 RETURN </
|