Formatted numeric output: Difference between revisions

Content deleted Content added
m Category:Simple, {{out}}
Line 1: Line 1:
{{task|Basic language learning}}[[Category:Text processing]]
{{task|Basic language learning}}[[Category:Text processing]] [[Category:Simple]]
Express a number in decimal as a fixed-length string with leading zeros.
Express a number in decimal as a fixed-length string with leading zeros.


Line 19: Line 19:
Put(Item => Value, Pic => Pic);
Put(Item => Value, Pic => Pic);
end Zero_Fill;</lang>
end Zero_Fill;</lang>
{{out}}
The output of this program is
<pre>

000037.25
000037.25
</pre>


=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
Line 47: Line 48:
printf(($4z-d.4de4dl$,r))
printf(($4z-d.4de4dl$,r))
)</lang>
)</lang>
{{out}}
Output:
<pre>
<pre>
+1.99990999791895e +1
+1.99990999791895e +1
Line 66: Line 67:


=={{header|AmigaE}}==
=={{header|AmigaE}}==
The function RealF can be used to convert a floating point value into a string, with a specified number of decimal digits. 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)
The function RealF can be used to convert a floating point value into a string, with a specified number of decimal digits.
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)
<lang amigae>PROC newRealF(es, fl, digit, len=0, zeros=TRUE)
<lang amigae>PROC newRealF(es, fl, digit, len=0, zeros=TRUE)
DEF s, t, i
DEF s, t, i
Line 129: Line 132:
ENDIF
ENDIF
= "-" + RIGHT$(STRING$(sl%,"0") + STR$(-n), sl%-1)</lang>
= "-" + RIGHT$(STRING$(sl%,"0") + STR$(-n), sl%-1)</lang>
{{out}}
Output:
<pre>
<pre>
00003.142
00003.142
Line 229: Line 232:
quit</lang>
quit</lang>


{{out}}
Output: <pre>Decimal: 00007.125
<pre>Decimal: 00007.125
Hexadecimal: 0000007.2
Hexadecimal: 0000007.2
Binary: 00111.001</pre>
Binary: 00111.001</pre>
Line 269: Line 273:
return 0;
return 0;
}</lang>
}</lang>
{{out}}
Output:
-7.125
-7.125
7.125
7.125
Line 444: Line 448:
]P</lang>
]P</lang>


{{out}}
Output:

<pre>Decimal: 00007.125
<pre>Decimal: 00007.125
Hexadecimal: 0000007.2
Hexadecimal: 0000007.2
Line 474: Line 477:
</lang>
</lang>


{{out}}
Result:
<pre>
<pre>
00007.125
00007.125
Line 581: Line 584:
printf(1,"%-09.3f\n",r)</lang>
printf(1,"%-09.3f\n",r)</lang>


{{out}}
Output:
<pre>
-7.125
-7.125
7.125
7.125
Line 588: Line 592:
00007.125
00007.125
7.125
7.125
</pre>



=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
Line 606: Line 610:


=={{header|Forth}}==
=={{header|Forth}}==
Forth has a rather rich set of number formatting words, which makes formatted output very flexible but sometime cumbersome.
Forth has a rather rich set of number formatting words,
which makes formatted output very flexible but sometime cumbersome.


Here one way to generate the required output.
Here one way to generate the required output. Note that the number generated is NOT truncated to the field width. If you wish to truncate the number, remove #s and 1- from the definition. (The 1- is necessary because #s always generates at least one digit, even if it's zero.)
Note that the number generated is NOT truncated to the field width.
If you wish to truncate the number, remove #s and 1- from the definition.
(The 1- is necessary because #s always generates at least one digit, even if it's zero.)


<lang forth>\ format 'n' digits of the double word 'd'
<lang forth>\ format 'n' digits of the double word 'd'
Line 642: Line 650:
<lang groovy>printf ("%09.3f", 7.125)</lang>
<lang groovy>printf ("%09.3f", 7.125)</lang>


{{out}}
Output:
<pre>00007.125</pre>
<pre>00007.125</pre>


Line 672: Line 680:
end</lang>
end</lang>


{{out}} Abbreviated
Abbreviated sample output:<pre>3.141592653589793 <=== no printf
<pre>3.141592653589793 <=== no printf
|3.141593| <=== sprintf |%r|
|3.141593| <=== sprintf |%r|
| 3.142| <=== sprintf |%9.3r|
| 3.142| <=== sprintf |%9.3r|
Line 697: Line 706:
}
}
}</lang>
}</lang>
{{out}}
Output:
<pre>000000007.125</pre>
<pre>000000007.125</pre>
Using <code>NumberFormat</code>:
Using <code>NumberFormat</code>:
Line 721: Line 730:
}
}
}</lang>
}</lang>
{{out}}
Output:
<pre>000000007.125
<pre>000000007.125
00007.12500
00007.12500
Line 739: Line 748:
=={{header|Lasso}}==
=={{header|Lasso}}==
<lang Lasso>7.125 -> asstring(-precision = 3, -padding = 9, -padchar = '0')</lang>
<lang Lasso>7.125 -> asstring(-precision = 3, -padding = 9, -padchar = '0')</lang>
{{out}}
Output:
<pre>00007.125</pre>
<pre>00007.125</pre>


=={{header|Liberty BASIC}}==
=={{header|Liberty BASIC}}==
Custom function builds on the supplied 'print using( "###.###", n)'.
Custom function builds on the supplied 'print using( "###.###", n)'.<br>
<br>
NB no check that this does not truncate high-order digits... and remember LB calculates with more figures than its normal 'print' displays.
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>
<lang lb>
Line 768: Line 776:
end function
end function
</lang>
</lang>
{{out}}
<pre>
Raw number =0.16045274 Using custom function =0000000000.16045<br>
Raw number =0.16045274 Using custom function =0000000000.16045<br>
Raw number =13221.2247 Using custom function =0000013221.22474<br>
Raw number =13221.2247 Using custom function =0000013221.22474<br>
Line 778: Line 788:
Raw number =818579.045 Using custom function =0000818579.04498<br>
Raw number =818579.045 Using custom function =0000818579.04498<br>
Raw number =81.460946 Using custom function =0000000081.46095<br>
Raw number =81.460946 Using custom function =0000000081.46095<br>
<pre>


=={{header|Logo}}==
=={{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.
Various collection functions, such as MAP and FILTER,
will work on individual characters of a string when given a word instead of a list.
<lang logo>to zpad :num :width :precision
<lang logo>to zpad :num :width :precision
output map [ifelse ? = "| | ["0] [?]] form :num :width :precision
output map [ifelse ? = "| | ["0] [?]] form :num :width :precision
Line 852: Line 864:
return
return
</lang>
</lang>
{{out}}
;Output
<pre>
<pre>
00007.125
00007.125
Line 867: Line 879:
echo((-r).format("09.3f"))
echo((-r).format("09.3f"))
echo(r.format("09.3f"))</lang>
echo(r.format("09.3f"))</lang>
{{out}}
Output:
<pre>7.1250000000000000e+00
<pre>7.1250000000000000e+00
-7.125
-7.125
Line 893: Line 905:


=={{header|Oz}}==
=={{header|Oz}}==
It is possible to set the precision used for float printing (where "precision" means the total number of digits used).
It is possible to set the precision used for float printing
(where "precision" means the total number of digits used).


It doesn't seem to be possible to use leading zeros for printing, so we implement this manually:
It doesn't seem to be possible to use leading zeros for printing,
so we implement this manually:
<lang oz>declare
<lang oz>declare
fun {PrintFloat X Prec}
fun {PrintFloat X Prec}
Line 964: Line 978:
End;
End;
End;</lang>
End;</lang>
{{out}}
Output:
<pre>
<pre>
1.200 000000001.200
1.200 000000001.200
Line 1,022: Line 1,036:
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)</lang>
print "e=%-09.4e f=%-09.4f g=%-09.4g!"%(r,r,r)</lang>
{{out}}
<pre>
<pre>
19.9990999792
19.9990999792
Line 1,044: Line 1,059:
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))</lang>
print("e={0:-09.4e} f={0:-09.4f} g={0:-09.4g}!".format(r))</lang>
{{out}}
<pre>
<pre>
19.9990999792
19.9990999792
Line 1,110: Line 1,126:
]
]


; REBOL has no built-in facilities for printing pictured
; REBOL has no built-in facilities for printing pictured output.
; output. However, it's not too hard to cook something up using the
; However, it's not too hard to cook something up using the
; string manipulation facilities.
; string manipulation facilities.


Line 1,142: Line 1,158:
print 7.125</lang>
print 7.125</lang>


{{out}}
Output:

<pre> -7.125
<pre> -7.125
7.125
7.125
Line 1,240: Line 1,255:
say 'u=' u
say 'u=' u
/*stick a fork in it, we're done.*/</lang>
/*stick a fork in it, we're done.*/</lang>
{{out}}
'''output'''
<pre style="height:30ex;overflow:scroll">
<pre style="height:30ex;overflow:scroll">
a= 7.125
a= 7.125
Line 1,325: Line 1,340:
end func;</lang>
end func;</lang>


{{out}}
Output:
<pre>
<pre>
7.125
7.125
Line 1,366: Line 1,381:
<lang Suneido>Print(7.125.Pad(9))</lang>
<lang Suneido>Print(7.125.Pad(9))</lang>


{{out}}
Output:
<pre>00007.125</pre>
<pre>00007.125</pre>


Line 1,399: Line 1,414:


t = <printf/'%09.3f' x></lang>
t = <printf/'%09.3f' x></lang>
{{out}}
output:
<pre>00007.125</pre>
<pre>00007.125</pre>


Line 1,422: Line 1,437:
=={{header|Vedit macro language}}==
=={{header|Vedit macro language}}==
Vedit only supports integers, but fixed point calculations can be used for decimal values.<br>
Vedit only supports integers, but fixed point calculations can be used for decimal values.<br>
The following example uses 3 decimal places (value scaled by 1000). The output is inserted at current edit position.
The following example uses 3 decimal places (value scaled by 1000).
The output is inserted at current edit position.
<lang vedit>#1 = 7125
<lang vedit>#1 = 7125
Num_Ins(#1, FILL+COUNT, 9) Char(-3) Ins_Char('.')</lang>
Num_Ins(#1, FILL+COUNT, 9) Char(-3) Ins_Char('.')</lang>


{{out}}
Output:
<pre>
<pre>
00007.125
00007.125