mirror of
https://gitlab.com/freepascal.org/fpc/source.git
synced 2025-04-20 18:09:27 +02:00
Format examples
This commit is contained in:
parent
afc81918c6
commit
e0dd9f3c91
@ -37,8 +37,9 @@ OBJECTS=ex1 ex2 ex3 ex4 ex5 ex6 ex7 ex8 ex9 ex10 ex11 ex12\
|
||||
ex25 ex26 ex27 ex28 ex29 ex30 ex31 ex32 ex33 ex34 ex35 ex36\
|
||||
ex37 ex38 ex39 ex40 ex41 ex42 ex43 ex44 ex45 ex46 ex47\
|
||||
ex48 ex49 ex50 ex51 ex52 ex53 ex54 ex55 ex56 ex57 ex58\
|
||||
ex59 ex60 ex61 ex62 ex63 ex64 ex65 ex66 ex67 ex68 ex68 ex70\
|
||||
# ex1 ex2 ex3 ex4 ex5 ex6 ex7 ex8 ex9 ex10
|
||||
ex59 ex60 ex61 ex62 ex63 ex64 ex65 ex66 ex67 ex68 ex69 ex70\
|
||||
ex71
|
||||
# ex2 ex3 ex4 ex5 ex6 ex7 ex8 ex9 ex10
|
||||
|
||||
TEXOBJECTS=$(addsuffix .tex, $(OBJECTS))
|
||||
|
||||
|
@ -67,3 +67,7 @@ ex64.pp contains an example of the BCDToInt function.
|
||||
ex65.pp contains an example of the CompareStr function.
|
||||
ex66.pp contains an example of the CompareText function.
|
||||
ex67.pp contains an example of the FloatToStr function.
|
||||
ex68.pp contains an example of the FloatToStrF function.
|
||||
ex69.pp contains an example of the FloatToText function.
|
||||
ex70.pp contains an example of the FmtStr function.
|
||||
ex71.pp contains an example of the Format function.
|
||||
|
39
docs/sysutex/ex68.pp
Normal file
39
docs/sysutex/ex68.pp
Normal file
@ -0,0 +1,39 @@
|
||||
Program Example68;
|
||||
|
||||
{ This program demonstrates the FloatToStrF function }
|
||||
|
||||
Uses sysutils;
|
||||
|
||||
Const Fmt : Array [TFloatFormat] of string[10] =
|
||||
('general','exponent','fixed','number','Currency');
|
||||
|
||||
Procedure Testit (Value : Extended);
|
||||
|
||||
Var I,J : longint;
|
||||
FF : TFloatFormat;
|
||||
|
||||
begin
|
||||
For I:=5 to 15 do
|
||||
For J:=1 to 4 do
|
||||
For FF:=ffgeneral to ffcurrency do
|
||||
begin
|
||||
Write (Value,'(Prec: ',I:2,', Dig: ',J,', fmt : ',Fmt[ff],') : ');
|
||||
Writeln (FloatToStrf(Value,FF,I,J));
|
||||
Write (-Value,'(Prec: ',I:2,', Dig: ',J,', fmt : ',Fmt[ff],') : ');
|
||||
Writeln (FloatToStrf(-Value,FF,I,J));
|
||||
end;
|
||||
end;
|
||||
|
||||
Begin
|
||||
Testit (1.1);
|
||||
Testit (1.1E1);
|
||||
Testit (1.1E-1);
|
||||
Testit (1.1E5);
|
||||
Testit (1.1E-5);
|
||||
Testit (1.1E10);
|
||||
Testit (1.1E-10);
|
||||
Testit (1.1E15);
|
||||
Testit (1.1E-15);
|
||||
Testit (1.1E100);
|
||||
Testit (1.1E-100);
|
||||
End.
|
42
docs/sysutex/ex69.pp
Normal file
42
docs/sysutex/ex69.pp
Normal file
@ -0,0 +1,42 @@
|
||||
Program Example68;
|
||||
|
||||
{ This program demonstrates the FloatToStrF function }
|
||||
|
||||
Uses sysutils;
|
||||
|
||||
Const Fmt : Array [TFloatFormat] of string[10] =
|
||||
('general','exponent','fixed','number','Currency');
|
||||
|
||||
Procedure Testit (Value : Extended);
|
||||
|
||||
Var I,J : longint;
|
||||
FF : TFloatFormat;
|
||||
S : ShortString;
|
||||
|
||||
begin
|
||||
For I:=5 to 15 do
|
||||
For J:=1 to 4 do
|
||||
For FF:=ffgeneral to ffcurrency do
|
||||
begin
|
||||
Write (Value,'(Prec: ',I:2,', Dig: ',J,', fmt : ',Fmt[ff],') : ');
|
||||
SetLength(S,FloatToText (@S[1],Value,FF,I,J));
|
||||
Writeln (S);
|
||||
Write (-Value,'(Prec: ',I:2,', Dig: ',J,', fmt : ',Fmt[ff],') : ');
|
||||
SetLength(S,FloatToText (@S[1],-Value,FF,I,J));
|
||||
Writeln (S);
|
||||
end;
|
||||
end;
|
||||
|
||||
Begin
|
||||
Testit (1.1);
|
||||
Testit (1.1E1);
|
||||
Testit (1.1E-1);
|
||||
Testit (1.1E5);
|
||||
Testit (1.1E-5);
|
||||
Testit (1.1E10);
|
||||
Testit (1.1E-10);
|
||||
Testit (1.1E15);
|
||||
Testit (1.1E-15);
|
||||
Testit (1.1E100);
|
||||
Testit (1.1E-100);
|
||||
End.
|
13
docs/sysutex/ex70.pp
Normal file
13
docs/sysutex/ex70.pp
Normal file
@ -0,0 +1,13 @@
|
||||
Program Example70;
|
||||
|
||||
{ This program demonstrates the FmtStr function }
|
||||
|
||||
Uses sysutils;
|
||||
|
||||
Var S : AnsiString;
|
||||
|
||||
Begin
|
||||
S:='';
|
||||
FmtStr (S,'For some nice examples of fomatting see %s.',['Format']);
|
||||
Writeln (S);
|
||||
End.
|
200
docs/sysutex/ex71.pp
Normal file
200
docs/sysutex/ex71.pp
Normal file
@ -0,0 +1,200 @@
|
||||
Program dof;
|
||||
|
||||
Uses sysutils;
|
||||
|
||||
Var P : Pointer;
|
||||
fmt,S : string;
|
||||
|
||||
Procedure TestInteger;
|
||||
|
||||
begin
|
||||
Try
|
||||
Fmt:='[%d]';S:=Format (Fmt,[10]);writeln(Fmt:12,' => ',s);
|
||||
Fmt:='[%%]';S:=Format (Fmt,[10]);writeln(Fmt:12,' => ',s);
|
||||
Fmt:='[%10d]';S:=Format (Fmt,[10]);writeln(Fmt:12,' => ',s);
|
||||
fmt:='[%.4d]';S:=Format (fmt,[10]);writeln(Fmt:12,' => ',s);
|
||||
Fmt:='[%10.4d]';S:=Format (Fmt,[10]);writeln(Fmt:12,' => ',s);
|
||||
Fmt:='[%0:d]';S:=Format (Fmt,[10]);writeln(Fmt:12,' => ',s);
|
||||
Fmt:='[%0:10d]';S:=Format (Fmt,[10]);writeln(Fmt:12,' => ',s);
|
||||
Fmt:='[%0:10.4d]';S:=Format (Fmt,[10]);writeln(Fmt:12,' => ',s);
|
||||
Fmt:='[%0:-10d]';S:=Format (Fmt,[10]);writeln(Fmt:12,' => ',s);
|
||||
Fmt:='[%0:-10.4d]';S:=Format (fmt,[10]);writeln(Fmt:12,' => ',s);
|
||||
Fmt:='[%-*.*d]';S:=Format (fmt,[4,5,10]);writeln(Fmt:12,' => ',s);
|
||||
except
|
||||
On E : Exception do
|
||||
begin
|
||||
Writeln ('Exception caught : ',E.Message);
|
||||
end;
|
||||
end;
|
||||
writeln ('Press enter');
|
||||
readln;
|
||||
end;
|
||||
|
||||
Procedure TestHexaDecimal;
|
||||
|
||||
begin
|
||||
try
|
||||
Fmt:='[%x]';S:=Format (Fmt,[10]);writeln(Fmt:12,' => ',s);
|
||||
Fmt:='[%10x]';S:=Format (Fmt,[10]);writeln(Fmt:12,' => ',s);
|
||||
Fmt:='[%10.4x]';S:=Format (Fmt,[10]);writeln(Fmt:12,' => ',s);
|
||||
Fmt:='[%0:x]';S:=Format (Fmt,[10]);writeln(Fmt:12,' => ',s);
|
||||
Fmt:='[%0:10x]';S:=Format (Fmt,[10]);writeln(Fmt:12,' => ',s);
|
||||
Fmt:='[%0:10.4x]';S:=Format (Fmt,[10]);writeln(Fmt:12,' => ',s);
|
||||
Fmt:='[%0:-10x]';S:=Format (Fmt,[10]);writeln(Fmt:12,' => ',s);
|
||||
Fmt:='[%0:-10.4x]';S:=Format (fmt,[10]);writeln(Fmt:12,' => ',s);
|
||||
Fmt:='[%-*.*x]';S:=Format (fmt,[4,5,10]);writeln(Fmt:12,' => ',s);
|
||||
except
|
||||
On E : Exception do
|
||||
begin
|
||||
Writeln ('Exception caught : ',E.Message);
|
||||
end;
|
||||
end;
|
||||
writeln ('Press enter');
|
||||
readln;
|
||||
end;
|
||||
|
||||
Procedure TestPointer;
|
||||
|
||||
begin
|
||||
P:=Pointer(1234567);
|
||||
try
|
||||
Fmt:='[0x%p]';S:=Format (Fmt,[P]);writeln(Fmt:12,' => ',s);
|
||||
Fmt:='[0x%10p]';S:=Format (Fmt,[P]);writeln(Fmt:12,' => ',s);
|
||||
Fmt:='[0x%10.4p]';S:=Format (Fmt,[P]);writeln(Fmt:12,' => ',s);
|
||||
Fmt:='[0x%0:p]';S:=Format (Fmt,[P]);writeln(Fmt:12,' => ',s);
|
||||
Fmt:='[0x%0:10p]';S:=Format (Fmt,[P]);writeln(Fmt:12,' => ',s);
|
||||
Fmt:='[0x%0:10.4p]';S:=Format (Fmt,[P]);writeln(Fmt:12,' => ',s);
|
||||
Fmt:='[0x%0:-10p]';S:=Format (Fmt,[P]);writeln(Fmt:12,' => ',s);
|
||||
Fmt:='[0x%0:-10.4p]';S:=Format (fmt,[P]);writeln(Fmt:12,' => ',s);
|
||||
Fmt:='[%-*.*p]';S:=Format (fmt,[4,5,P]);writeln(Fmt:12,' => ',s);
|
||||
except
|
||||
On E : Exception do
|
||||
begin
|
||||
Writeln ('Exception caught : ',E.Message);
|
||||
end;
|
||||
end;
|
||||
writeln ('Press enter');
|
||||
readln;
|
||||
end;
|
||||
|
||||
Procedure TestString;
|
||||
|
||||
begin
|
||||
try
|
||||
Fmt:='[%s]';S:=Format(fmt,['This is a string']);Writeln(fmt:12,'=> ',s);
|
||||
fmt:='[%0:s]';s:=Format(fmt,['This is a string']);Writeln(fmt:12,'=> ',s);
|
||||
fmt:='[%0:18s]';s:=Format(fmt,['This is a string']);Writeln(fmt:12,'=> ',s);
|
||||
fmt:='[%0:-18s]';s:=Format(fmt,['This is a string']);Writeln(fmt:12,'=> ',s);
|
||||
fmt:='[%0:18.12s]';s:=Format(fmt,['This is a string']);Writeln(fmt:12,'=> ',s);
|
||||
fmt:='[%-*.*s]';s:=Format(fmt,[18,12,'This is a string']);Writeln(fmt:12,'=> ',s);
|
||||
except
|
||||
On E : Exception do
|
||||
begin
|
||||
Writeln ('Exception caught : ',E.Message);
|
||||
end;
|
||||
end;
|
||||
writeln ('Press enter');
|
||||
readln;
|
||||
end;
|
||||
|
||||
Procedure TestExponential;
|
||||
|
||||
begin
|
||||
Try
|
||||
Fmt:='[%e]';S:=Format (Fmt,[1.234]);writeln(Fmt:12,' => ',s);
|
||||
Fmt:='[%10e]';S:=Format (Fmt,[1.234]);writeln(Fmt:12,' => ',s);
|
||||
Fmt:='[%10.4e]';S:=Format (Fmt,[1.234]);writeln(Fmt:12,' => ',s);
|
||||
Fmt:='[%0:e]';S:=Format (Fmt,[1.234]);writeln(Fmt:12,' => ',s);
|
||||
Fmt:='[%0:10e]';S:=Format (Fmt,[1.234]);writeln(Fmt:12,' => ',s);
|
||||
Fmt:='[%0:10.4e]';S:=Format (Fmt,[1.234]);writeln(Fmt:12,' => ',s);
|
||||
Fmt:='[%0:-10e]';S:=Format (Fmt,[1.234]);writeln(Fmt:12,' => ',s);
|
||||
Fmt:='[%0:-10.4e]';S:=Format (fmt,[1.234]);writeln(Fmt:12,' => ',s);
|
||||
Fmt:='[%-*.*e]';S:=Format (fmt,[4,5,1.234]);writeln(Fmt:12,' => ',s);
|
||||
except
|
||||
On E : Exception do
|
||||
begin
|
||||
Writeln ('Exception caught : ',E.Message);
|
||||
end;
|
||||
end;
|
||||
writeln ('Press enter');
|
||||
readln;
|
||||
end;
|
||||
|
||||
Procedure TestNegativeExponential;
|
||||
|
||||
begin
|
||||
Try
|
||||
Fmt:='[%e]';S:=Format (Fmt,[-1.234]);writeln(Fmt:12,' => ',s);
|
||||
Fmt:='[%10e]';S:=Format (Fmt,[-1.234]);writeln(Fmt:12,' => ',s);
|
||||
Fmt:='[%10.4e]';S:=Format (Fmt,[-1.234]);writeln(Fmt:12,' => ',s);
|
||||
Fmt:='[%0:e]';S:=Format (Fmt,[-1.234]);writeln(Fmt:12,' => ',s);
|
||||
Fmt:='[%0:10e]';S:=Format (Fmt,[-1.234]);writeln(Fmt:12,' => ',s);
|
||||
Fmt:='[%0:10.4e]';S:=Format (Fmt,[-1.234]);writeln(Fmt:12,' => ',s);
|
||||
Fmt:='[%0:-10e]';S:=Format (Fmt,[-1.234]);writeln(Fmt:12,' => ',s);
|
||||
Fmt:='[%0:-10.4e]';S:=Format (fmt,[-1.234]);writeln(Fmt:12,' => ',s);
|
||||
Fmt:='[%-*.*e]';S:=Format (fmt,[4,5,-1.234]);writeln(Fmt:12,' => ',s);
|
||||
except
|
||||
On E : Exception do
|
||||
begin
|
||||
Writeln ('Exception caught : ',E.Message);
|
||||
end;
|
||||
end;
|
||||
writeln ('Press enter');
|
||||
readln;
|
||||
end;
|
||||
|
||||
Procedure TestSmallExponential;
|
||||
|
||||
begin
|
||||
Try
|
||||
Fmt:='[%e]';S:=Format (Fmt,[0.01234]);writeln(Fmt:12,' => ',s);
|
||||
Fmt:='[%10e]';S:=Format (Fmt,[0.01234]);writeln(Fmt:12,' => ',s);
|
||||
Fmt:='[%10.4e]';S:=Format (Fmt,[0.01234]);writeln(Fmt:12,' => ',s);
|
||||
Fmt:='[%0:e]';S:=Format (Fmt,[0.01234]);writeln(Fmt:12,' => ',s);
|
||||
Fmt:='[%0:10e]';S:=Format (Fmt,[0.01234]);writeln(Fmt:12,' => ',s);
|
||||
Fmt:='[%0:10.4e]';S:=Format (Fmt,[0.01234]);writeln(Fmt:12,' => ',s);
|
||||
Fmt:='[%0:-10e]';S:=Format (Fmt,[0.0123]);writeln(Fmt:12,' => ',s);
|
||||
Fmt:='[%0:-10.4e]';S:=Format (fmt,[0.01234]);writeln(Fmt:12,' => ',s);
|
||||
Fmt:='[%-*.*e]';S:=Format (fmt,[4,5,0.01234]);writeln(Fmt:12,' => ',s);
|
||||
except
|
||||
On E : Exception do
|
||||
begin
|
||||
Writeln ('Exception caught : ',E.Message);
|
||||
end;
|
||||
end;
|
||||
writeln ('Press enter');
|
||||
readln;
|
||||
end;
|
||||
|
||||
Procedure TestSmallNegExponential;
|
||||
|
||||
begin
|
||||
Try
|
||||
Fmt:='[%e]';S:=Format (Fmt,[-0.01234]);writeln(Fmt:12,' => ',s);
|
||||
Fmt:='[%10e]';S:=Format (Fmt,[-0.01234]);writeln(Fmt:12,' => ',s);
|
||||
Fmt:='[%10.4e]';S:=Format (Fmt,[-0.01234]);writeln(Fmt:12,' => ',s);
|
||||
Fmt:='[%0:e]';S:=Format (Fmt,[-0.01234]);writeln(Fmt:12,' => ',s);
|
||||
Fmt:='[%0:10e]';S:=Format (Fmt,[-0.01234]);writeln(Fmt:12,' => ',s);
|
||||
Fmt:='[%0:10.4e]';S:=Format (Fmt,[-0.01234]);writeln(Fmt:12,' => ',s);
|
||||
Fmt:='[%0:-10e]';S:=Format (Fmt,[-0.01234]);writeln(Fmt:12,' => ',s);
|
||||
Fmt:='[%0:-10.4e]';S:=Format (fmt,[-0.01234]);writeln(Fmt:12,' => ',s);
|
||||
Fmt:='[%-*.*e]';S:=Format (fmt,[4,5,-0.01234]);writeln(Fmt:12,' => ',s);
|
||||
except
|
||||
On E : Exception do
|
||||
begin
|
||||
Writeln ('Exception caught : ',E.Message);
|
||||
end;
|
||||
end;
|
||||
writeln ('Press enter');
|
||||
readln;
|
||||
end;
|
||||
|
||||
begin
|
||||
TestInteger;
|
||||
TestHexadecimal;
|
||||
TestPointer;
|
||||
TestExponential;
|
||||
TestNegativeExponential;
|
||||
TestSmallExponential;
|
||||
TestSmallNegExponential;
|
||||
end.
|
Loading…
Reference in New Issue
Block a user