Formatted numeric output: Difference between revisions

Content deleted Content added
No edit summary
Root (talk | contribs)
review →‎Pascal
Line 19: Line 19:
00007.125
00007.125
</pre>
</pre>

=={{header|Ada}}==
=={{header|Ada}}==
<lang ada>with Ada.Text_Io.Editing; use Ada.Text_Io.Editing;
<lang ada>with Ada.Text_Io.Editing; use Ada.Text_Io.Editing;
Line 367: Line 368:
00007.125
00007.125
7.125
7.125
=={{header|C++}}==


=={{header|C++}}==
<lang cpp>#include <iostream>
<lang cpp>#include <iostream>
#include <iomanip>
#include <iomanip>
Line 799: Line 800:
00007.125
00007.125
</pre>
</pre>

=={{header|Free Pascal}}==
''See [[#Pascal|Pascal]]''


=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
Line 1,200: Line 1,204:
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.
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.
<lang logo>print form 7.125 -1 "|%09.3f| ; 00007.125</lang>
<lang logo>print form 7.125 -1 "|%09.3f| ; 00007.125</lang>

=={{header|Lua}}==
=={{header|Lua}}==
<lang lua>function digits(n) return math.floor(math.log(n) / math.log(10))+1 end
<lang lua>function digits(n) return math.floor(math.log(n) / math.log(10))+1 end
Line 1,349: Line 1,354:


=={{header|Pascal}}==
=={{header|Pascal}}==
<lang pascal>procedure writeInFixedFormat(n: real);
See [[Formatted_numeric_output#Delphi | Delphi]]
const
wholeNumberPlaces = 5;
fractionalPlaces = 3;
zeroDigit = '0';
negative = '-';
var
signPresent: boolean;
i: integer;
begin
// NOTE: This does not catch “negative” zero.
signPresent := n < 0.0;
if signPresent then
begin
write(negative);
n := abs(n);
end;
// determine number of leading zeros
i := wholeNumberPlaces;
if n > 0 then
begin
i := i - trunc(ln(n) / ln(10));
end;
for i := i - 1 downto succ(ord(signPresent)) do
begin
write(zeroDigit);
end;
// writes n with
// - at least wholeNumberPlaces characters in total
// - exactly fractionalPlaces post-radix digits
// rounded
write(n:wholeNumberPlaces:fractionalPlaces);
end;</lang>


=={{header|Perl}}==
=={{header|Perl}}==