Formatted numeric output: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
(Dialects of BASIC moved to the BASIC section.) |
||
Line 333: | Line 333: | ||
Same output as the C code. |
Same output as the C code. |
||
=={{header| |
=={{header|BASIC}}== |
||
==={{header|BaCon}}=== |
|||
BaCon can use C style <tt>printf</tt> format specifiers. |
BaCon can use C style <tt>printf</tt> format specifiers. |
||
Line 344: | Line 345: | ||
00007.125</pre> |
00007.125</pre> |
||
=={{header|BBC BASIC}}== |
==={{header|BBC BASIC}}=== |
||
<syntaxhighlight lang="bbcbasic"> PRINT FNformat(PI, 9, 3) |
<syntaxhighlight lang="bbcbasic"> PRINT FNformat(PI, 9, 3) |
||
PRINT FNformat(-PI, 9, 3) |
PRINT FNformat(-PI, 9, 3) |
||
Line 361: | Line 362: | ||
-0003.142 |
-0003.142 |
||
</pre> |
</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|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|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|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|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}}== |
=={{header|bc}}== |
||
Line 1,036: | Line 1,193: | ||
=={{header|Free Pascal}}== |
=={{header|Free Pascal}}== |
||
''See [[#Pascal|Pascal]]'' |
''See [[#Pascal|Pascal]]'' |
||
=={{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|Fōrmulæ}}== |
=={{header|Fōrmulæ}}== |
||
Line 1,168: | Line 1,300: | ||
print, n, format='(f08.3)' |
print, n, format='(f08.3)' |
||
;==> 0007.125</syntaxhighlight> |
;==> 0007.125</syntaxhighlight> |
||
=={{header|IS-BASIC}}== |
|||
<syntaxhighlight lang="is-basic">100 LET F=7.125 |
|||
110 PRINT USING "-%%%%%.###":F</syntaxhighlight> |
|||
=={{header|J}}== |
=={{header|J}}== |
||
Line 1,361: | Line 1,489: | ||
{{out}} |
{{out}} |
||
<pre>00007.125</pre> |
<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. |
|||
<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.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}}== |
=={{header|Logo}}== |
||
Line 1,711: | Line 1,799: | ||
or by invoking <code>ToString</code> on the number: |
or by invoking <code>ToString</code> on the number: |
||
<syntaxhighlight lang="powershell">7.125.ToString('00000.000')</syntaxhighlight> |
<syntaxhighlight lang="powershell">7.125.ToString('00000.000')</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|Python}}== |
=={{header|Python}}== |
||
Line 2,008: | Line 2,092: | ||
puts " %09.3f" % -r #=> -0007.125 |
puts " %09.3f" % -r #=> -0007.125 |
||
puts " %+09.3f" % r #=> +0007.125</syntaxhighlight> |
puts " %+09.3f" % r #=> +0007.125</syntaxhighlight> |
||
=={{header|Run BASIC}}== |
|||
<syntaxhighlight lang="runbasic">print right$("00000";using("#####.##",7.125),8) ' => 00007.13</syntaxhighlight> |
|||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
Line 2,162: | Line 2,243: | ||
format "%08.3f" $number</syntaxhighlight> |
format "%08.3f" $number</syntaxhighlight> |
||
Use with <tt>puts</tt> if output is desired to go to a channel. |
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.}} |
|||
<syntaxhighlight lang="ti89b">right("00000" & format(7.12511, "f3"), 9)</syntaxhighlight> |
|||
=={{header|Toka}}== |
=={{header|Toka}}== |
||
Line 2,210: | Line 2,285: | ||
7.125 |
7.125 |
||
</pre> |
</pre> |
||
=={{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|Vedit macro language}}== |
=={{header|Vedit macro language}}== |
||
Line 2,267: | Line 2,292: | ||
<syntaxhighlight lang="vedit">#1 = 7125 |
<syntaxhighlight lang="vedit">#1 = 7125 |
||
Num_Ins(#1, FILL+COUNT, 9) Char(-3) Ins_Char('.')</syntaxhighlight> |
Num_Ins(#1, FILL+COUNT, 9) Char(-3) Ins_Char('.')</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
00007.125 |
00007.125 |
||
</pre> |
</pre> |
||
=={{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}}== |
=={{header|Wren}}== |
||
Line 2,320: | Line 2,334: | ||
"%09d".fmt(7.125) //-->"000000007" |
"%09d".fmt(7.125) //-->"000000007" |
||
"%09,d".fmt(78901.125)//-->"00078,901"</syntaxhighlight> |
"%09,d".fmt(78901.125)//-->"00078,901"</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> |